first commit
This commit is contained in:
commit
a66604eb6c
52 changed files with 790 additions and 0 deletions
7
Enums/Cargo.lock
generated
Normal file
7
Enums/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "Enums"
|
||||
version = "0.1.0"
|
||||
6
Enums/Cargo.toml
Normal file
6
Enums/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "Enums"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
5
Enums/README.md
Normal file
5
Enums/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# benefits of Enums with match:
|
||||
|
||||
- compiler ensures all variants are handled
|
||||
- clean and readable code
|
||||
- can include data directly in the enum
|
||||
35
Enums/src/main.rs
Normal file
35
Enums/src/main.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// An enum defines a type by enumerating its possible values. EAch variant can optionally carry
|
||||
// data
|
||||
|
||||
enum Message {
|
||||
Quit, // this is no data
|
||||
Move {
|
||||
//structure
|
||||
x: i32,
|
||||
y: i32,
|
||||
},
|
||||
|
||||
Write(String), // string that can be written
|
||||
ChangeColor(i32, i32, i32), // three integers, for the rgb
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let m1 = Message::Move { x: 1, y: 2 };
|
||||
let m2 = Message::Quit;
|
||||
let m3 = Message::Write(String::from("safal lama"));
|
||||
let m4 = Message::ChangeColor(255, 0, 0);
|
||||
|
||||
handle(m1);
|
||||
handle(m2);
|
||||
handle(m3);
|
||||
handle(m4);
|
||||
}
|
||||
|
||||
fn handle(msg: Message) {
|
||||
match msg {
|
||||
Message::Quit => println!("quit message received"),
|
||||
Message::Move { x, y } => println!("moving ({}, {})", x, y),
|
||||
Message::Write(txt) => println!("message: {}", txt),
|
||||
Message::ChangeColor(r, g, b) => println!("Color: ({},{},{})", r, g, b),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue