Skip to content

Instantly share code, notes, and snippets.

@NebulaFox
Created March 20, 2020 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NebulaFox/ddf29b2505ab57bded9348db817730b1 to your computer and use it in GitHub Desktop.
Save NebulaFox/ddf29b2505ab57bded9348db817730b1 to your computer and use it in GitHub Desktop.
variable decleration
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