I’ve just read this blog post from 2015 about enums
and matches
. In the conclusion there are four points which are not covered by the blog post and reader is encouraged to consult the documentation. Unfortunately I wasn’t able to find much information about those topics in it.
Thus I would like to ask here about these points.
- how to say
Higher
instead ofAnswer::Higher
in a pattern
If I understand this right, it is possible to omit the enum name in the matching block. So this example
enum MyEnum {A, B}
match e {
MyEnum::A => "A",
MyEnum::B => "B"
}
could be become something like this:
match e {
A => "A",
B => "B"
}
- defining new named constants
What is meant by this?
- binding via
ident @ pattern
Where is the advantage using @
instead of the matched value itself?
let x = 1;
match x {
e @ 1 ... 5 => println!("got a range element {}", e),
// in comparison to
1 ... 5 => println!("got a range element {}", x),
}
- the potentially subtle difference between
{ let id = expr; ... }
versusmatch expr { id => { ... } }
What are those subtle differences?