Created
August 26, 2020 20:14
-
-
Save FeezyHendrix/374c9eb9e0e4018f8a212df3b5eb69c4 to your computer and use it in GitHub Desktop.
Vectors, HashMaps Casting If-Let, While-Let Result Enum in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
// using the as keyword you can cast a type to another | |
let f = 21.32424242_f32; | |
let i = f as u8; | |
let c = i as char; | |
println!("{} {} {}", f, i , c); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
fn main() { | |
let mut hm = HashMap::new(); | |
hm.insert("ket", 12); | |
hm.insert("dv", 12); | |
hm.remove("dv"); | |
for (k, v) in &hm { | |
println!("{}, {}", k , v) | |
} | |
match hm.get("ket") { | |
Some(&n) => println!("{}", n), | |
_ => println!("no match") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let s = Some('c'); | |
match s { | |
Some(i) => println!("{}", i), | |
_ => {} | |
} | |
if let Some(i) = s { | |
println!("{}", i); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Result<T, E> { | |
Ok(T), | |
Err(E) | |
} | |
fn main() { | |
let x = some(10); | |
match x { | |
Some(x) => if() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
enum Example { | |
Float(f64), | |
Int(i32), | |
Text(String) | |
} | |
fn main() { | |
let r = vec![ | |
Example::Float(33.3), | |
Example::Int(32), | |
Example::Text(String::from("I love school")) | |
]; | |
println!("{:?}", r); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
// You can define a vector using the vec! macro | |
let x = vec![1, 2, 3, 4]; | |
// You can also use this to define a vector | |
let mut v: Vec<i32> = Vec::new(); | |
v.push(32); | |
v.push(12); | |
v.push(1); | |
for i in &v { | |
println!("{}", i); | |
} | |
println!("{} , {}", v.len(), v.capacity()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut s = Some(0); | |
while let Some(i) = s { | |
if i > 19 { | |
println!("Quit"); | |
s = None; | |
} else { | |
println!("{}", i); | |
s = Some(i + 2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment