Skip to content

Instantly share code, notes, and snippets.

@metatoaster
Last active May 31, 2022 09:20
Show Gist options
  • Select an option

  • Save metatoaster/8b36dc0f9f95bd8366addc08c3f276e8 to your computer and use it in GitHub Desktop.

Select an option

Save metatoaster/8b36dc0f9f95bd8366addc08c3f276e8 to your computer and use it in GitHub Desktop.
def match_value(number: int) -> str:
three = 3
match number:
case 1:
return "Yay you are number wan"
case 2:
return "Second mouse gets the cheese"
case three:
return "You came third"
case 42:
return "The answer to life, the universe, and everything"
case _:
return "You are too late"
print(match_value(3))
#[allow(unreachable_patterns,unused_variables)]
fn main() {
fn match_value(number: i64) -> String {
let three = 3;
match number {
1 => "Yay you are number wan",
2 => "Second mouse gets the cheese",
three => "You came third",
42 => "The answer to life, the universe, and everything",
_ => "You are too late",
}.to_string()
}
println!("{}", match_value(3));
}
$ python3.10 match_expr.py
File "/tmp/match_expr.py", line 8
case three:
^^^^^
SyntaxError: name capture 'three' makes remaining patterns unreachable
$ cargo run
Compiling match_expr v0.1.0 (/tmp/match_expr)
Finished dev [unoptimized + debuginfo] target(s) in 0.16s
Running `target/debug/match_expr`
You came third
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment