Rust FromStr trait is defined like this
pub trait FromStr {
type Err;
fn from_str(s: &str) -> Result<Self, Self::Err>;
}
It does not name its lifetime and one cannot implement that trait for something containing reference to source string, for example:
struct MyIterator<'a> {
cur_pointer: &'a str
}
impl<'a> FromStr for MyIterator<'a> {
type Err = i32;
fn from_str(s: &'a str) -> Result<Self, Self::Err> {
Ok(MyIterator { cur_pointer: s })
}
}
gives error
method `from_str` has an incompatible type for trait: expected bound lifetime parameter , found concrete lifetime [E0053]
By far I found no way to implement FromStr for MyIterator. I assume that is because original trait did not expose string's lifetime in its parameters. My first question is: am I right that there is no way to implement FromStr for MyIterator? If I'm wrong, what is a way to do it (assuming MyIterator wants to keep reference to original string)?
By now I found only this question: How do I implement FromStr with a concrete lifetime?, but best answer starts with "I don't believe that you can implement", so I want to be sure this is really impossible in Rust 1.0.0.
Now, if trait definition was like that:
trait MyFromStr<'a> {
type Err;
fn from_str(s: &'a str) -> Result<Self, Self::Err>;
}
one could implement it for types containing references to original string and not containing references to original string:
struct MyIterator<'a> {
cur_pointer: &'a str
}
impl<'a> MyFromStr<'a> for MyIterator<'a> {
type Err = i32;
fn from_str(s: &'a str) -> Result<Self, Self::Err> {
Ok(MyIterator { cur_pointer: s })
}
}
struct MyCopy {
val: String
}
impl<'a> MyFromStr<'a> for MyCopy {
type Err = i32;
fn from_str(s: &'a str) -> Result<Self, Self::Err> {
Ok(MyCopy { val: s.to_string() })
}
}
My second question is: is there any particular reason that trait FromStr does not expose lifetime? Maybe I misunderstand something about lifetimes and exposing lifetime has drawbacks?