Closed
Description
openedon Aug 8, 2022
Given the following code:
fn foo(bar: &mut usize) {
todo!()
}
fn main() {
foo(&mut Default::default()); // Works
foo(Default::default()); // Doesn't
}
The current output is:
error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `&mut usize: Default` is not satisfied
--> src/main.rs:7:9
|
7 | foo(Default::default()); // Doesn't
| --- ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `&mut usize`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `Default`:
f32
f64
i128
i16
i32
i64
i8
isize
and 6 others
For more information about this error, try `rustc --explain E0277`.
This output is confusing, because changing the type to e.g. i64 won't solve the problem and instead cause a lot of new ones. For a HashMap, I currently get the suggestion to use a HashSet. The correct solution would be to recognize that usize: Default
and suggest to add a &mut
. This may generalize to other similar situations (&
) or traits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment