Created
March 20, 2020 03:40
-
-
Save NebulaFox/ddf29b2505ab57bded9348db817730b1 to your computer and use it in GitHub Desktop.
variable decleration
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 convert<S>(option: Option<S>) -> Option<String> | |
where | |
S: Into<String>, | |
{ | |
match option { | |
Some(string) => Some(string.into()), | |
None => None | |
} | |
} | |
fn is_ferris<S>(option: Option<S>) -> bool | |
where | |
S: AsRef<str>, | |
{ | |
match option { | |
Some(string) => string.as_ref() == "Ferris", | |
None => false | |
} | |
} | |
fn main() { | |
let ferris: Option<String> = Some("Ferris").map(|s| s.into()); | |
let world = convert(Some("World")); | |
let array: &[Option<String>] = &[None, ferris, world]; | |
for o in array.iter() { | |
match o { | |
Some(string) => println!("Hello {}", string), | |
None => println!("PANIC!") | |
} | |
} | |
let ferris: Option<&str> = Some("Ferris").map(|s| s.as_ref()); | |
let world: Option<String> = Some(String::from("World")); | |
println!("Is it Ferris: none: {}, ferris: {}, world: {}", | |
is_ferris(None::<&str>), | |
is_ferris(ferris), | |
is_ferris(world) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment