fn main() {
let v: bool = true;
// with negation:
if !&v {
panic!();
}
}
but this one isn't?
fn main() {
let v: bool = true;
// without negation:
if &v {
panic!();
}
}
The latter results in:
error[E0308]: mismatched types
--> not_neg.rs:3:8
|
3 | if &v {
| ^^ expected `bool`, found `&bool`
|
help: consider removing the borrow
|
3 - if &v {
3 + if v {
|
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
I see, thanks. It just seemed a bit odd/inconsistent that one is valid, but not the other. Any insights on the purpose of implementing Not for &bool? Perhaps to avoid having to explicitly dereference?
Generic code, same as things like Add<&i32> for i32.
People might want to write code that works on Add<&T> to avoid copies if the thing they're adding is expensive to copy, and having these impls means that simple stuff like bool and i32 work in those cases too.