本文介绍了FromStr 特性不暴露生命周期的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rust FromStr trait 是这样定义的

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 })
    }
}

报错

method `from_str` has an incompatible type for trait: expected bound lifetime parameter , found concrete lifetime [E0053]

到目前为止,我没有找到为 MyIterator 实现 FromStr 的方法.我认为这是因为原始特征没有在其参数中公开字符串的生命周期.我的第一个问题是:没有办法为 MyIterator 实现 FromStr 是对的吗?如果我错了,有什么方法可以做到(假设 MyIterator 想要保留对原始字符串的引用)?

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)?

现在我只发现了这个问题:我该怎么做用具体的生命周期实现 FromStr?,但最好的答案是我不相信你能实现",所以我想确定这在 Rust 1.0.0 中真的是不可能的.

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() })
    }
}

我的第二个问题是: trait FromStr 不暴露生命周期有什么特别的原因吗?也许我对生命周期有一些误解,暴露生命周期有缺点?

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?

推荐答案

包含生命周期会使 trait 更加复杂,更具体地说,使使用该 trait 的泛型代码更加冗长(携带不需要的生命周期).fn foo不再工作.

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.

对于字符串的预期用途也没有必要.通常你从一个字符串中解析并使用结果而不用担心字符串.以这种方式编码当然更容易.许多类型无论如何都不需要生命周期(请参阅实现它的一长串类型).

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).

您不能为这种类型实现 FromStr.你能做的是

You can't implement FromStr for this type. What you can do is

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

这为您提供了许多通用转换机制,而不是方法 str::parse().也许那个方法的名字是省略生命周期的另一个论据.

This gives you a lot of generic conversion machinery, just not the method str::parse(). Perhaps the name of that method is another argument for the omission of the lifetime.

这篇关于FromStr 特性不暴露生命周期的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:24