简体   繁体   English

为什么即使Pattern同时具有&str和char的实现,Split类型也只返回&str?

[英]Why does the Split type only return &str even though Pattern has implementations for both &str and char?

I'm having a hard time understanding how the Split type works in Rust. 我很难理解Split类型在Rust中的工作方式。

Split<'a, P> where P: Pattern<'a> is the type returned by the std::string::String::split method. Split<'a, P> where P: Pattern<'a>std::string::String::split方法返回的类型。 The type has an implementation for Iterator<'a, P> , where P is still the Pattern type, but in reality (and as I would expect) the Iterator only returns &str slices. 该类型具有Iterator<'a, P> ,其中P仍然是Pattern类型,但实际上(并且正如我所期望的那样), Iterator仅返回&str切片。

Eg, split(p).collect::<Vec<&str>>() works but split(p).collect::<Vec<char>>() results a compiler error. 例如, split(p).collect::<Vec<&str>>()可以工作,但是split(p).collect::<Vec<char>>()导致编译器错误。 This is what I would expect to happen, but I don't understand how it happens since Pattern has implementations for both &str and char . 这是我希望发生的事情,但是由于Pattern同时为&strchar实现,所以我不知道它是如何发生的。

Why isn't the Split type simply defined as Split<'a, &'a str> , since it is effectively an Iterator over &str s? 为什么不将Split类型简单定义为Split<'a, &'a str> ,因为它实际上是对&strIterator Why does it behave as if it is effectively defined as that? 为什么它的行为好像被有效地定义为那样?

The type has an implementation for Iterator<'a, P> 该类型具有Iterator<'a, P>

It doesn't. 没有。 It's just Iterator , which has no type parameters. 只是Iterator ,没有类型参数。 Every implementation of Iterator must declare the type of the item that it iterates over using an associated type. Iterator每个实现都必须使用关联的类型声明要Iterator的项的类型。 For example, Split 's implementation [1] is something like this: 例如, Split的实现[1]是这样的:

impl <'a, P> Iterator for Split<'a, P> {
    type Item = &'a str;
    fn next(&mut self) -> Option<&'a str> { ... }
}

Why isn't the Split type simply defined as Split<'a, &'a str> , since it is effectively an Iterator over &str s? 为什么不将Split类型简单定义为Split<'a, &'a str> ,因为它实际上是对&strIterator

Because iterators are lazy. 因为迭代器是惰性的。 The Split struct still needs to know about the pattern in order to match the next item. Split结构仍需要了解模式才能匹配下一项。 Its iterator instance has Item = &str , because that is what it iterates over. 它的迭代器实例具有Item = &str ,因为这是对其进行迭代的内容。


[1] The actual implementation is generated by a macro . [1]实际的实现是由宏生成的

暂无
暂无

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

相关问题 mypy 抱怨:带有 Type[TypeVar[&#39;T&#39;, str, date]] 和 T 输出的函数类型注释:不兼容的返回值类型(得到“str”,预期为“date”) - mypy complains: function type annotation with Type[TypeVar['T', str, date]] and T output: Incompatible return value type (got "str", expected "date") Swift - 为什么即使 where 子句已经专门化了关联类型,协议仍然被视为泛型? - Swift - Why is protocol still treated as generic even though where clause has specialized the associated type? 在 rust 中,“ToString 的通用毯式实现”是否与“用于 char/str/String 的 impl Tostring...”冲突 - Does the 'generic blanket impl of ToString' conflict with the 'impl Tostring for char/str/String…' in rust 即使返回类型是 IEnumerable 也无法产生<u></u> - Cannot yield even though the return type is IEnumerable<U> 为什么“类型参数从不使用”,即使它在 where 子句中使用? - Why is the "type parameter never used" even though it is used in a where clause? 即使关联类型不同,特征实现也有冲突 - Conflicting trait implementations even though associated types differ 怎么接受Vec <String> 和Vec <str> 作为Rust中的函数arg - How to accept both Vec<String> and Vec<str> as function arg in Rust 带有可编码返回错误的通用返回类型即使 Xcode 正确显示 output 上的返回类型,也无法推断通用参数“T” - Generic return type with Codable returning error Generic parameter 'T' could not be inferred even though Xcode correctly shows return type on output 为什么这个类编译即使它没有正确实现其接口? - Why does this class compile even though it does not properly implement its interface? 无法将 `std::str::Chars` 分配给类型为 `I: Iterator` 的变量 - Can't assign `std::str::Chars` to a variable of type `I: Iterator`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM