Rust supports both imperative (separate commands on separate lines) and functional styles. Functional style allows you to chain multiple methods together in a single statement, often making code more concise. For better readability, you can place each method on a new line.
Commonly used methods for chaining include:
.into_iter(): Creates an iterator that takes ownership of the items (gives owned values, not references)..skip(n): Skips the first n items..take(n): Takes the next n items..collect::<Type>(): Transforms the iterator back into a collection (e.g., Vec<T>). You must specify the target type using the turbofish syntax ::<Type> if it cannot be inferred.
let my_vec = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let new_vec = my_vec
.into_iter() // iterate over the items
.skip(3) // skip over three items: 0, 1, and 2
.take(4) // take the next four: 3, 4, 5, and 6
.collect::<Vec<i32>>(); // put them in a new Vec<i32>