trait

Extend — add items to an existing collection

Use extend when a destination already exists. The collection defines how it incorporates incoming values. A Vec appends them in order. Expected stdout (not executed): ["first", "second", "third"] Common mistake: Replacing the existing collection with collect when the old items are needed.

Formal Syntax & Structure

rust syntaxrust
trait Extend<A> { fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T); /* provided methods */ }

Working Example

Example Snippetrust
fn main() {    let mut queue = vec!["first"];    queue.extend(["second", "third"]);    println!("{queue:?}");}
Rust Reference