Skip to content

Instantly share code, notes, and snippets.

@dgabriele
Last active December 8, 2021 01:12
Show Gist options
  • Save dgabriele/2689acc4f8a8336acc2811a3f99d7d89 to your computer and use it in GitHub Desktop.
Save dgabriele/2689acc4f8a8336acc2811a3f99d7d89 to your computer and use it in GitHub Desktop.
Optional Arguments in Rust
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