为什么我可以在对迭代器的可变引用上调用 take()?

Why am I allowed to call take() on a mutable reference to an iterator?

我试图在迭代器上使用 take(n) 来处理第一个 n 项,然后对其余项执行不同的操作(for ... in ... 循环)。

在声明中:

fn take(self, n: usize) -> Take<Self>

作为第一个参数的 self 告诉我 take() 必须用于拥有的值,而不是引用 (&self) 或可变引用 (&mut self) .

by_ref() 的文档说它 returns 是一个可变引用,

This is useful to allow applying iterator adaptors while still retaining ownership of the original iterator.

它包括一个使用 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 可以是拥有的类型或不可变引用或可变引用。您可以在对 Iterators 的可变引用上调用 take 的原因是 because of this generic blanket impl which implements the Iterator trait on all mutable references to Iterators:

impl<'_, I> Iterator for &'_ mut I where I: Iterator + ?Sized { /* implementation */ }

让我们用vec.into_iter()作为一个具体的例子。因为它 returns std::vec::IntoIter 实现了 Iterator 我们知道 take 的这个实现必须存在:

take(self: std::vec::IntoIter, n: usize) -> Take<std::vec::IntoIter> { /* implementation */ }

但是,我们也知道 &mut std::vec::IntoItertake 的实现必须存在,因为它会由上面提到的通用 blanket impl 自动生成,并且该实现的签名如下所示:

take(self: &mut std::vec::IntoIter, n: usize) -> Take<&mut std::vec::IntoIter> { /* implementation */ }

这就是为什么您可以对任何实现 Iterator.

的任何类型的可变引用调用 take 的原因