如何实现具有生命周期的特征一般实现特征

How to implement trait with lifetime generically that implements trait

如何执行以下操作?

trait Foo {}
trait Bar<'a> {}

impl<S> S for Bar<'_> where S: Foo {}

确实有@user4815162342的回答:

impl<'a, S> Bar<'a> for S where S: Foo {}

您还可以为特征添加默认实现:

trait Bar {
    pub fn hello() {
        println!("Hello, world!");
    }
}

struct Foo {}

impl Bar for Foo {}

// ...
Foo::hello();