Trait mismatch for function argument

前端 未结 2 535
孤独总比滥情好
孤独总比滥情好 2021-01-22 15:09

I\'ve got one piece of Rust code that compiles and one that\'s very similar that does not.

The one that works:

pub fn do_something(_: Box

        
2条回答
  •  不思量自难忘°
    2021-01-22 15:48

    To be honest, I'm no expert in Rust at all, but my expectation would have been that both of the snippets you show do not compile. That is because, as you pointed out, Iterator is a trait and not a type and basically you want do_something to receive any type which implements Iterator. Maybe there exists a shortcut such that the compiler can transform the signature into a generic if one of the types is a trait which could be why is sometimes works, but then I'm also not familiar with the Rust language specification enough.

    Instead of having do_something take something of type Iterator (?) make it a generic of type T where T is trait bound.

    pub fn do_something(_: Box>) 
        where T: Iterator + Send {}
    
    fn main() {
        let iter = Box::new(Box::new(vec![1.0].into_iter()));
        do_something(iter);
    }
    

    Playground

    Alternatively, you constrain do_something entirely to std::vec::IntoIter and only take parameters of that type.

    pub fn do_something(_: Box>>) {}
    
    fn main() {
        let iter = Box::new(Box::new(vec![1.0].into_iter()));
        do_something(iter);
    }
    

    Playground

提交回复
热议问题