Trait std::iter::Extend1.0.0[][src]

pub trait Extend<A> {
    fn extend<T>(&mut self, iter: T)
    where
        T: IntoIterator<Item = A>
; fn extend_one(&mut self, item: A) { ... }
fn extend_reserve(&mut self, additional: usize) { ... } }
Expand description

用迭代器的内容扩展集合。

迭代器产生一系列值,并且集合也可以视为一系列值。 Extend trait 弥补了这一差距,使您可以通过包含该迭代器的内容来扩展集合。 当使用已经存在的键扩展集合时,该条目将被更新; 如果集合允许多个具有相同键的条目,则将插入该条目。

Examples

基本用法:

// 您可以使用一些字符扩展 String:
let mut message = String::from("The first three letters are: ");

message.extend(&['a', 'b', 'c']);

assert_eq!("abc", &message[29..32]);
Run

实现 Extend:

// 一个样本集合,这只是 Vec<T> 的包装
#[derive(Debug)]
struct MyCollection(Vec<i32>);

// 让我们给它一些方法,以便我们可以创建一个方法并向其中添加一些东西。
impl MyCollection {
    fn new() -> MyCollection {
        MyCollection(Vec::new())
    }

    fn add(&mut self, elem: i32) {
        self.0.push(elem);
    }
}

// 由于 MyCollection 包含 i32 的列表,因此我们为 i32 实现 Extend
impl Extend<i32> for MyCollection {

    // 使用具体的类型签名,这要简单一些: 我们可以调用扩展为可转换为 It32 的 Iterator 的任何内容。
    // 因为我们需要将 i32 放入 MyCollection 中。
    fn extend<T: IntoIterator<Item=i32>>(&mut self, iter: T) {

        // 实现非常简单: 遍历迭代器,然后将每个元素 add() 传递给我们自己。
        for elem in iter {
            self.add(elem);
        }
    }
}

let mut c = MyCollection::new();

c.add(5);
c.add(6);
c.add(7);

// 让我们用三个数字扩展集合
c.extend(vec![1, 2, 3]);

// 我们已经将这些元素添加到最后
assert_eq!("MyCollection([5, 6, 7, 1, 2, 3])", format!("{:?}", c));
Run

Required methods

用迭代器的内容扩展集合。

由于这是此 trait 唯一需要的方法,因此 trait-level 文档包含更多详细信息。

Examples

基本用法:

// 您可以使用一些字符扩展 String:
let mut message = String::from("abc");

message.extend(['d', 'e', 'f'].iter());

assert_eq!("abcdef", &message);
Run

Provided methods

🔬 This is a nightly-only experimental API. (extend_one #72631)

用一个元素扩展一个集合。

🔬 This is a nightly-only experimental API. (extend_one #72631)

在集合中为给定数量的附加元素保留容量。

默认实现不执行任何操作。

Implementors

🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)

扩展将引用中的元素复制到 Vec 之前的实现。

此实现专用于切片迭代器,它使用 copy_from_slice 一次附加整个切片。

🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)

插入迭代器中的所有新键值,并用迭代器返回的新值替换现有键中的值。

🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)
🔬 This is a nightly-only experimental API. (extend_one #72631)