49 lines
1.2 KiB
Rust
49 lines
1.2 KiB
Rust
enum Direction
|
|
{
|
|
Up,
|
|
Down,
|
|
Left,
|
|
Right
|
|
}
|
|
|
|
enum Status
|
|
{
|
|
Succeeded(String),
|
|
Failed(String)
|
|
}
|
|
|
|
struct RGB
|
|
{
|
|
red: i32,
|
|
green: i32,
|
|
blue: i32,
|
|
}
|
|
fn main() {
|
|
// normal Enum
|
|
let direction = Direction::Down;
|
|
match direction {
|
|
Direction::Up => println!("Going up"),
|
|
Direction::Down => println!("Going down"),
|
|
Direction::Left => println!("Going left"),
|
|
Direction::Right => println!("Going right"),
|
|
}
|
|
|
|
// enum holding data
|
|
let status = Status::Failed(String::from("Authentication Failed"));
|
|
match status
|
|
{
|
|
Status::Succeeded(message) => println!("Succeeded with msg: {}",message),
|
|
Status::Failed(message) => println!("Failed with msg: {}",message),
|
|
}
|
|
|
|
// struct
|
|
let color = RGB{red:255,green:255,blue:255};
|
|
println!("color has R:{} G:{} B:{}",color.red,color.green,color.blue);
|
|
|
|
// tuples
|
|
let pixel = (100,100,RGB{red:0xff,green:0xff,blue:0xff});
|
|
println!("pixel tuple: x:{} y:{} color:{}.{}.{}",pixel.0,pixel.1,pixel.2.red,pixel.2.green,pixel.2.blue);
|
|
//tuple unpacking
|
|
let (xcoord, ycoord, color) = pixel;
|
|
println!("unpacking pixel tuple: x:{} y:{} color:{}.{}.{}",xcoord,ycoord,color.red,color.green,color.blue);
|
|
} |