Skip to content

Instantly share code, notes, and snippets.

@vijairaj
Forked from rust-play/playground.rs
Created December 13, 2018 15:12
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 vijairaj/7852fb9fb025a58f8d73119f8f20b5b4 to your computer and use it in GitHub Desktop.
Save vijairaj/7852fb9fb025a58f8d73119f8f20b5b4 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn print(n: Option<i32>) {
// conditionally destructure
if let Some(inner) = n {
println!("Val: {}", inner);
}
}
fn main() {
print(None);
print(Some(0));
print(Some(5));
}
@vijairaj
Copy link
Author

if let syntax is confusing at first but the core idea is that it's conditional let.

In the example inner is assigned only when n is not None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment