I have this array
let buffer: &[u8] = &[0; 40000];
But when I want to map it like this:
*buffer.map( |x| 0xff);
I have the following error:
error[E0599]: no method named `map` found for type `&[u8]` in the current scope
--> src/bin/save_png.rs:12:13
|
12 | *buffer.map( |x| 0xff); //.map(|x| 0xff);
| ^^^
|
= note: the method `map` exists but the following trait bounds were not satisfied:
`&mut &[u8] : std::iter::Iterator`
`&mut [u8] : std::iter::Iterator`
I tried several ways to do the elements mutable but I can't get the right syntax. Anyone have experience? I'm trying to work with a png image buffer.
The type &[T] doesn't have a map method. If you look at the error message, it is telling you that a method called map exists but it won't work for &mut &[u8] or &mut [u8] because those types do not implement Iterator. Arrays and other collections usually have a method, or set of methods, for creating an iterator. For a slice or an array, you have the choice of either iter() (iterating over references) or into_iter() (iterating over moved values and consuming the source collection).
Usually, you'll also want to collect the values into some other collection:
let res: Vec<u8> = buffer
.iter()
.map(|x| 0xff)
.collect();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With