|
|
|
|
|
by steveklabnik
414 days ago
|
|
> This is the actual C++ translation of the opening code in TFA: No, it isn't: this is iterating over references, not moving. This is equivalent to fn main() {
let x = vec![1, 2];
for y in &x {
println!("{}", y);
}
println!("{}", x.len());
}
in Rust. Note the &, just like in your C++. |
|
> This is straightforward: we create a vector containing the values [1, 2], then iterate over it and print each element, and then finally print out the length of the vector. This is the kind of code people write every day.
The C++ code I provided does essentially this (I omitted printing the length, since it is so trivial), and is "the kind of code people write every day".
The fact that Rust requires you to consider move semantics for such simple code is precisely one of the central points of the article.