Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add matches #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,77 @@ fn main() {
// float is used for decimals
let float = 0.32;

// println!("Different numbers => {}, {}, {}", unsigned, signed, float);
println!("Different numbers => {}, {}, {}", unsigned, signed, float);

// char is used for single character
let character = 'a';
// println!("Character => {}", character);
println!("Character => {}", character);

// boolean is used for true or false
let boolean = true;
// println!("Boolean => {}", boolean);
println!("Boolean => {}", boolean);

// tuple is used for grouping different data types
let tuple = (1, -2, 3.0, 4, true);
// println!("Tuple => {:?}", tuple);
println!("Tuple => {:?}", tuple);

// array is used for grouping same data types
let array = [1, 2, 3, 4, 5];
// println!("Array => {:?}", array);
println!("Array => {:?}", array);

// string is used for grouping characters
let string = "Hello World";
// println!("String => {}", string);
println!("String => {}", string);

// vector is used for grouping same data types and it is dynamic
let mut vector = vec![1, 2, 3, 4, 5];
vector.push(6);
// println!("Vector => {:?}", vector);
println!("Vector => {:?}", vector);

// hash map is used for grouping 2 different data types as key value pair
let mut hash_map = std::collections::HashMap::new();
hash_map.insert("Solana", 100);
hash_map.insert("age", 2);
// println!("Hash Map => {:?}", hash_map);

// enums
println!("Hash Map => {:?}", hash_map);

// enums and match
enum Color {
Red,
Green,
Blue,
Red,
Green,
Blue,
RGB(u8, u8, u8), // tuple struct variant
}
// Simple Match
let color = Color::RGB(0, 0, 255);
match color {
Color::Red => println!("Red"),
Color::Green => println!("Green"),
Color::Blue => println!("Blue"),
Color::RGB(r, g, b) => println!("RGB: {}, {}, {}", r, g, b),
}

// Range match
let match_number = 5;
match match_number {
1..=5 => println!("one through five"),
_ => println!("match_number is not within 1 to 5!"),
}

// Match with combined conditions
let match_pair = (0, -1);
match match_pair {
(0, y) => println!("First is 0 and y is {:?}", y),
(x, 0) => println!("x is {:?} and last is 0", x),
_ => println!("Neither X nor Y is 0")
}
// hash set is used for grouping same data types

let mut hash_set = std::collections::HashSet::new();

hash_set.insert("John Doe");
hash_set.insert("Jane Doe");

// println!("Hash Set => {:?}", hash_set);
println!("Hash Set => {:?}", hash_set);

// Looping

Expand Down