Rust 如何为trait实现指定引用生存期?

Rust 如何为trait实现指定引用生存期?,rust,Rust,如何为结构Foo实现TraitFoo #[derive(Debug)] struct Foo<'f> { os: Option<&'f str> } impl<'f> Foo<'f> { fn new(x: &'f str) -> Foo<'f> { Foo { os:Some(x) } } } trait Trait

如何为结构Foo实现TraitFoo

#[derive(Debug)]
struct Foo<'f> {
    os: Option<&'f str>
}

impl<'f> Foo<'f> {
    fn new(x: &'f str) -> Foo<'f> {
        Foo {
            os:Some(x)
        }       
    }
}

trait TraitFoo {
    fn foo(x:&str) -> Self;
}

impl<'f> TraitFoo for Foo<'f> {
    fn foo(x: &str) -> Foo<'f> {
        Foo {
            os:Some(x)
        }
    }
}

fn main() {
    println!("{:?}", Foo::new("one"));
    println!("{:?}", Foo::foo("two"));
}
有没有办法为
Foo
实现
TraitFoo


关于目的:

我试图用错误出现的指定位置创建自己的错误类。我需要类似的特性将标准错误转换为我的错误:

pub trait FromWhere<'a,T>:std::convert::From<T> {
    fn from_where(T, &'a str) -> Self;
}

impl<'e,T:ApplyWrpErrorTrait+std::convert::From<T>+'static> FromWhere<'e,T> for WrpError<'e> {
  fn from_where(err: T, whr:&'e str) -> WrpError<'e> {
      if whr!="" {
          WrpError{
              kind: ErrorKind::Wrapped,
              descr: None,
              pos: Some(whr),
              cause: Some(Box::new(err))
          }
      }else{
          std::convert::From::from(err)
      }
  }
}
pub trait FromWhere Self;
}
从何处导入{

fn from_where(err:T,whr:&'e str)->wrperor您还需要指定特征的寿命

trait TraitFoo<'a> {
    fn foo(x: &'a str) -> Self;
}

impl<'a> TraitFoo<'a> for Foo<'a> {
    fn foo(x:&'a str) -> Foo<'a> {
        Foo{
            os:Some(x)
        }
    }
}
TraitFoo自我特质;
}

impl for Foo Foo您可以添加一个使用此特性的泛型函数的示例吗?此特性的用途是什么?仅此而已。无法在此处添加代码,因此我正在编辑问题以回答@sellibitze注释
pub trait FromWhere<'a,T>:std::convert::From<T> {
    fn from_where(T, &'a str) -> Self;
}

impl<'e,T:ApplyWrpErrorTrait+std::convert::From<T>+'static> FromWhere<'e,T> for WrpError<'e> {
  fn from_where(err: T, whr:&'e str) -> WrpError<'e> {
      if whr!="" {
          WrpError{
              kind: ErrorKind::Wrapped,
              descr: None,
              pos: Some(whr),
              cause: Some(Box::new(err))
          }
      }else{
          std::convert::From::from(err)
      }
  }
}
trait TraitFoo<'a> {
    fn foo(x: &'a str) -> Self;
}

impl<'a> TraitFoo<'a> for Foo<'a> {
    fn foo(x:&'a str) -> Foo<'a> {
        Foo{
            os:Some(x)
        }
    }
}