Reputation: 1195
enum Error {}
fn foo<T>(bars: Vec<T>) -> Result<Vec<T>, Error>
{
let mut baz = vec![];
for bar in bars.iter(){
baz.push(bar);
}
Ok(baz)
}
nothing like & or * seems to lead to a solution, also because bar
is an external crate
enum Error {}
fn foo<T>(bars: Vec<T>) -> Result<Vec<T>, Error>
{
let mut baz = vec![];
for &bar in bars.iter(){
baz.push(bar);
}
Ok(*baz)
}
what is causing the issue? How would it work?
Upvotes: 0
Views: 118
Reputation: 1195
the problem was, that .iter()
always returns referenced values.
the reference reveals the following:
There are three common methods which can create iterators from a collection:
iter(), which iterates over &T.
iter_mut(), which iterates over &mut T.
into_iter(), which iterates over T.
enum Error {}
fn foo<T>(bars: Vec<T>) -> Result<Vec<T>, Error>
{
let mut baz = vec![];
for bar in bars.into_iter(){
baz.push(bar);
}
Ok(baz)
}
Upvotes: 1