I saw the following line in my code, and I am not sure what it does as I haven't encountered the @ operator before.
if let e@Err(_) = changed {
...
}
Can this line be written without the @ operator, what would that look like?
I saw the following line in my code, and I am not sure what it does as I haven't encountered the @ operator before.
if let e@Err(_) = changed {
...
}
Can this line be written without the @ operator, what would that look like?
It's a way to bind the matched value of a pattern to a variable(using the syntax: variable @ subpattern). For example,
let x = 2;
match x {
e @ 1 ..= 5 => println!("got a range element {}", e),
_ => println!("anything"),
}
According to https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html#-bindings and https://doc.rust-lang.org/reference/patterns.html#identifier-patterns, it is used for simultaneously matching to the pattern to the right of the @ while also binding the value to the identifier to the left of the @.
To answer your second question, Yes, it would look like
if let Err(_) = &changed {
// continue to use `changed` like you would use `e`
}
Note that in order to continue to use changed inside the body, you need to match for the reference &changed. Otherwise it will be moved and discarded (unless it happens to be Copy).