This doesn’t compile:
fn foo(items: &mut [u8]) -> u8 {
// The centent of this function isn’t important
items.sort();
42
}
fn main() {
let mut x: Vec<Vec<u8>> = vec![
vec![0, 1, 2],
vec![3, 4, 5],
];
let x = x
.iter_mut()
.map(foo) // <-- this line
println!("{:?}", x);
}
error[[E0631]](https://doc.rust-lang.org/stable/error-index.html#E0631): type mismatch in function arguments
--> src/main.rs:20:14
|
1 | fn foo(items: &mut [u8]) -> u8 {
| ------------------------------ found signature defined here
...
20 | .map(foo);
| --- ^^^ expected due to this
| |
| required by a bound introduced by this call
|
= note: expected function signature `fn(&mut Vec<u8>) -> _`
found function signature `for<'r> fn(&'r mut [u8]) -> _`
note: required by a bound in `map`
For more information about this error, try `rustc --explain E0631`.
The solution is either to call first deref
.map(std::ops::DerefMut::deref_mut).map(foo)
or
.map(|items| items.as_mut())
Or to use a lambda
.map(|mut items| foo(&mut items))
The current error doesn’t help to understand what the issue is and it’s very confusing because it may look like a lifetime error (because of the for<'r>…).
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c6ac2ca7a31b126442978d30419e07a8
This doesn’t compile:
The solution is either to call first deref
or
Or to use a lambda
The current error doesn’t help to understand what the issue is and it’s very confusing because it may look like a lifetime error (because of the
for<'r>…).https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c6ac2ca7a31b126442978d30419e07a8