简体   繁体   English

如何实现具有生命周期的 trait 实现 trait

[英]How to implement trait with lifetime generically that implements trait

How do I do the following?我如何执行以下操作?

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

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

There is indeed @user4815162342's answer:确实有@user4815162342 的回答:

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

You can also add default implementations to your trait:您还可以将默认实现添加到您的 trait:

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

struct Foo {}

impl Bar for Foo {}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM