Last active
December 8, 2021 01:12
-
-
Save dgabriele/2689acc4f8a8336acc2811a3f99d7d89 to your computer and use it in GitHub Desktop.
Optional Arguments 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 say_hello(name: Option<&String>) { | |
let greeting = match name { | |
Some(name_str) => format!("Hello, {}!", name_str), | |
None => "You don't have a name!" | |
}; | |
println!(greeting); | |
} | |
// called like: | |
let name = String::from("Jeff"); | |
say_hello(Some(&name)); | |
// or | |
say_hello(None); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment