简体   繁体   English

FromStr特性不暴露生命的原因是什么?

[英]What are reasons for FromStr trait not exposing lifetime?

Rust FromStr trait is defined like this Rust FromStr特征定义如下

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. 到目前为止,我发现没有办法为MyIterator实现FromStr。 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? 我的第一个问题是:我是对的,没有办法为MyIterator实现FromStr? If I'm wrong, what is a way to do it (assuming MyIterator wants to keep reference to original string)? 如果我错了,有什么方法可以做到(假设MyIterator想要保持对原始字符串的引用)?

By now I found only this question: How do I implement FromStr with a concrete lifetime? 到目前为止,我只发现了这个问题: 如何实现具有生命周期的FromStr? , 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. ,但最好的答案始于“我不相信你可以实现”,所以我想确保在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? 我的第二个问题是:是否有任何特殊原因,特征FromStr不会暴露生命? Maybe I misunderstand something about lifetimes and exposing lifetime has drawbacks? 也许我误解了一些关于生命的事情并且暴露生命有缺点?

Including the lifetime makes the trait more complicated and, more specifically, makes generic code using that trait more verbose (carrying around an unneeded lifetime). 包括生命周期使得特征更复杂,更具体地说,使得使用该特征的通用代码更加冗长(携带不必要的生命周期)。 fn foo<T: FromStr> would no longer work . fn foo<T: FromStr>不再有效

It's also unnecessary for the intended uses of strings. 字符串的预期用途也是不必要的。 Usually you parse from a string and use the result without worrying about the string. 通常你从字符串解析并使用结果而不用担心字符串。 It's certainly easier to code that way. 以这种方式编码肯定更容易。 Many types don't need the lifetime anyway (see the long list of types implementing it). 许多类型无论如何都不需要生命周期(请参阅实现它的类型的长列表)。

You can't implement FromStr for this type. 您无法为此类型实现FromStr What you can do is 你能做的是

impl<'a> From<&'a str> for MyIterator<'a> { ... }

This gives you a lot of generic conversion machinery, just not the method str::parse() . 这给了你很多通用的转换机制,而不是方法str::parse() Perhaps the name of that method is another argument for the omission of the lifetime. 也许该方法的名称是遗漏生命的另一个论据。

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

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