Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

rust - How do I implement FromStr with a concrete lifetime?

I want to implement FromStr for a struct with a lifetime parameter:

use std::str::FromStr;

struct Foo<'a> {
    bar: &'a str,
}

impl<'a> FromStr for Foo<'a> {
    type Err = ();
    fn from_str(s: &str) -> Result<Foo<'a>, ()> {

        Ok(Foo { bar: s })
    }
}

pub fn main() {
    let foo: Foo = "foobar".parse().unwrap();
}

However, the compiler complains:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/main.rs:11:12
   |
11 |         Ok(Foo { bar: s })
   |            ^^^
   |
help: consider using an explicit lifetime parameter as shown: fn from_str(s: &'a str) -> Result<Foo<'a>, ()>
  --> src/main.rs:9:5
   |
9  |     fn from_str(s: &str) -> Result<Foo<'a>, ()> {
   |     ^

Changing the impl to

impl<'a> FromStr for Foo<'a> {
    type Err = ();
    fn from_str(s: &'a str) -> Result<Foo<'a>, ()> {
        Ok(Foo { bar: s })
    }
}

gives this error

error[E0308]: method not compatible with trait
  --> src/main.rs:9:5
   |
9  |     fn from_str(s: &'a str) -> Result<Foo<'a>, ()> {
   |     ^ lifetime mismatch
   |
   = note: expected type `fn(&str) -> std::result::Result<Foo<'a>, ()>`
   = note:    found type `fn(&'a str) -> std::result::Result<Foo<'a>, ()>`
note: the anonymous lifetime #1 defined on the block at 9:51...
  --> src/main.rs:9:52
   |
9  |     fn from_str(s: &'a str) -> Result<Foo<'a>, ()> {
   |                                                    ^
note: ...does not necessarily outlive the lifetime 'a as defined on the block at 9:51
  --> src/main.rs:9:52
   |
9  |     fn from_str(s: &'a str) -> Result<Foo<'a>, ()> {
   |                                                    ^
help: consider using an explicit lifetime parameter as shown: fn from_str(s: &'a str) -> Result<Foo<'a>, ()>
  --> src/main.rs:9:5
   |
9  |     fn from_str(s: &'a str) -> Result<Foo<'a>, ()> {
   |     ^

Playpen

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't believe that you can implement FromStr in this case.

fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err>;

There's nothing in the trait definition that ties the lifetime of the input to the lifetime of the output.

Not a direct answer, but I'd just suggest making a constructor that accepts the reference:

struct Foo<'a> {
    bar: &'a str
}

impl<'a> Foo<'a> {
    fn new(s: &str) -> Foo {
        Foo { bar: s }
    }
}

pub fn main() {
    let foo = Foo::new("foobar"); 
}

This has the side benefit of there not being any failure modes - no need to unwrap.

You could also just implement From:

struct Foo<'a> {
    bar: &'a str,
}

impl<'a> From<&'a str> for Foo<'a> {
    fn from(s: &'a str) -> Foo<'a> {
        Foo { bar: s }
    }
}

pub fn main() {
    let foo: Foo = "foobar".into();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...