What are the differences between an impl trait argument and generic function parameter?

前端 未结 2 1734
渐次进展
渐次进展 2020-12-15 18:36

impl Traits can be used as function arguments. Are there differences between this and a generic function with a trait constraint?

trait Foo {}

fn func1(_: i         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 18:57

    Both produce identical assembly, at least with the following simple test case:

    trait Foo {}
    
    struct Bar;
    
    impl Foo for Bar {}
    
    fn func1(_: impl Foo) {}
    
    fn func2(_: T) {}
    
    fn main() {
        let x = Bar;
    
        let y = func1(x); // or func2(x);
    }
    

提交回复
热议问题