til
til

Reputation: 1195

in loop / assignment: expected type parameter `T`, found `&T`

error on playground

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

Answers (1)

til
til

Reputation: 1195

solution in playground

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

Related Questions