Last active
December 11, 2020 06:44
-
-
Save frencojobs/dbee29ecd6a3c3ebbd6461f5c983ba86 to your computer and use it in GitHub Desktop.
I made Intuitive Rust Examples that made me understand the concepts of Option
This file contains hidden or 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
// difference between and_then vs map for `Option` | |
fn increment(n: i32) -> Option<i32> { | |
Some(n).map(|x| x + 1) | |
} | |
fn main() { | |
let one = Some(1); | |
let four = one | |
.map(|one| one + 1) | |
.map(|two| two + 1) | |
.and_then(|three| increment(three)); | |
println!("{}", four.unwrap()); // 4 | |
} |
This file contains hidden or 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
// the point of using `as_ref` for `Option` | |
fn this_wont_work() { | |
let lower: Option<String> = Some(String::from("case")); | |
let upper: Option<String> = lower.map(|x| x.to_uppercase()); | |
println!("UPPER: {}", upper.unwrap()); | |
println!("lower: {}", lower.unwrap()); // lower is moved, can't be used | |
} | |
fn this_work() { | |
let lower: Option<String> = Some(String::from("case")); | |
let upper: Option<String> = lower.as_ref().map(|x| x.to_uppercase()); | |
println!("UPPER: {}", upper.unwrap()); | |
println!("lower: {}", lower.unwrap()); | |
} | |
fn main() { | |
this_wont_work(); | |
this_work(); | |
} |
This file contains hidden or 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
mod gates { | |
pub fn and() { | |
let x = Some("x"); | |
let y = None::<&str>; | |
println!("x & y = {}", x.and(y).unwrap_or("y")); | |
println!("y & x = {}", y.and(x).unwrap_or("y")); | |
println!("x & x = {}", x.and(x).unwrap_or("None")); | |
println!("y & y = {}", y.and(y).unwrap_or("None")); | |
} | |
pub fn or() { | |
let x = Some("x"); | |
let y = None::<&str>; | |
println!("x | y = {}", x.or(y).unwrap_or("y")); | |
println!("y | x = {}", y.or(x).unwrap_or("y")); | |
println!("x | x = {}", x.or(x).unwrap_or("None")); | |
println!("y | y = {}", y.or(y).unwrap_or("None")); | |
} | |
pub fn xor() { | |
let x = Some("x"); | |
let y = None::<&str>; | |
println!("x ⊕ y = {}", x.xor(y).unwrap_or("y")); | |
println!("y ⊕ x = {}", y.xor(x).unwrap_or("y")); | |
println!("x ⊕ x = {}", x.xor(x).unwrap_or("None")); | |
println!("y ⊕ y = {}", y.xor(y).unwrap_or("None")); | |
} | |
} | |
fn main() { | |
println!("x = Some(x)"); | |
println!("y = None"); | |
println!(); | |
gates::and(); | |
println!(); | |
gates::or(); | |
println!(); | |
gates::xor(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment