I have a somewhat basic trait
trait Dup<T> {
fn dup(&mut self) -> Vec<T>;
}
with an implementation to "duplicate" element. For example:
impl<'a, F, T> Dup<T> for Map<Iter<'a, T>, F>
where F: FnMut(&'a T) -> T,
T: Clone
{
fn dup(&mut self) -> Vec<T> {
let mut tot = vec![];
for int in self {
tot.push(int.clone());
tot.push(int);
}
tot
}
}
It can easily be used like that:
let ints = vec![1, 2]
.iter()
.map(|e| e + 1)
.dup(); // [2, 2, 3, 3]
which is the expected result.
But for some reason, it seams that when there is an object that doesn't implements Copy
, this doesnt work anymore.
let strings = vec![String::new()]
.iter()
.map(|e| e)
.dup(); // PANICS
with the following error:
error[E0599]: the method `dup` exists for struct `Map<Iter<'_, String>, ...>`, but its trait bounds were not satisfied
--> src/main.rs:35:10
|
32 | let strings = vec![String::new()]
| ___________________-
33 | | .iter()
34 | | .map(|e| e)
| | --- doesn't satisfy `<_ as FnOnce<(&String,)>>::Output = String`
35 | | .dup();
| | -^^^ method cannot be called on `Map<Iter<'_, String>, ...>` due to unsatisfied trait bounds
| |_________|
|
|
::: /home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/map.rs:62:1
|
62 | pub struct Map<I, F> {
| -------------------- doesn't satisfy `_: Dup<String>`
|
The error seems a bit mysterious to me, and even tho I tried multiple times to fix it, I can't really find a way of doing so. It's probably something like "String doesnt implements Copy" problem, but I would like it to work without Copy.
Does someone has a clue to do that ?