问题描述
我试图在迭代器上使用 take(n)
对前 n
项执行某些操作,然后对其余项执行不同的操作(for ... in ...
循环)。>
在声明中:
fn take(self,n: usize) -> Take<Self>
作为第一个参数的 self
告诉我 take()
必须用于拥有的值,而不是引用 (&self
) 或可变引用 (&mut self
)。
by_ref()
的文档说它返回一个可变引用,并且
这对于允许应用迭代器适配器时仍然有用 保留原始迭代器的所有权。
它包含一个使用 by_ref().take(n)
的示例,然后继续使用迭代器(就像我想要的那样)。
我的问题是,为什么允许我在可变引用上调用 take()
(而不是要求拥有的值)?换句话说,take()
不应该被声明为:
fn take(&mut self,n: usize) -> Take<Self>
是否可以将其与可变引用一起使用?
我应该寻找什么声明才能告诉我这是可能的?
解决方法
Rust 将 T
、&T
和 &mut T
视为不同的类型。作为独立的类型,我们可以为每个类型实现不同的方法。示例:
struct Struct;
trait Printable {
fn print(self);
}
impl Printable for Struct {
fn print(self) {
println!("I'm an owned Struct");
}
}
impl Printable for &Struct {
fn print(self) {
println!("I'm a Struct reference");
}
}
fn main() {
let s = Struct;
let s_ref = &Struct;
s.print();
s_ref.print();
}
如果我们对 trait 方法进行脱糖,我们得到:
fn print(self: Self) { /* implementation */ }
其中 Self
等于实现类型,在 Struct
的情况下为:
fn print(self: Struct) { /* implementation */ }
在 &Struct
的情况下是:
fn print(self: &Struct) { /* implementation */ }
所以实际上 self
可以是一个拥有的类型或一个不可变的引用或一个可变的引用。您可以对 take
的可变引用调用 Iterator
的原因是 because of this generic blanket impl which implements the Iterator
trait on all mutable references to Iterator
s:
impl<'_,I> Iterator for &'_ mut I where I: Iterator + ?Sized { /* implementation */ }
让我们使用 vec.into_iter()
作为一个具体的例子。由于它返回实现 std::vec::IntoIter
的 Iterator
,我们知道 take
的这种实现必须存在:
take(self: std::vec::IntoIter,n: usize) -> Take<std::vec::IntoIter> { /* implementation */ }
但是,我们也知道 take
的 &mut std::vec::IntoIter
实现必须存在,因为它会由上面提到的通用毯式实现自动生成,并且该实现的签名如下所示:>
take(self: &mut std::vec::IntoIter,n: usize) -> Take<&mut std::vec::IntoIter> { /* implementation */ }
这就是为什么您可以对实现 take
的任何类型的任何可变引用调用 Iterator
。