In this chapter, we’re going to see a lot of Rust’s functional style, which is based on expressions. This style lets you use a method that gives an output, that output becomes the next method’s input, and you repeat until you have the final output that you want. It works like a chain of methods, which is why people call it method chaining. Method chaining is a lot of fun once you get used to it; it lets you do a lot of work with not very much code. Iterators and closures are a big help here, so they are the main focus of this chapter.
Rust is a systems programming language like C and C++, and its code can be written as separate commands in separate lines, but it also has a functional style. Both styles are okay, but functional style is usually shorter.
fn main() {
let mut new_vec = Vec::new();
let mut counter = 1;
loop {
new_vec.push(counter);
counter += 1;
if counter == 10 {
break;
}
}
println!("{new_vec:?}");
}