Description
In section 17. Generics, some example code is given:
fn map<T, U>(vector: &[T], function: |v: &T| -> U) -> Vec<U> {
let mut accumulator = Vec::new();
for element in vector.iter() {
accumulator.push(function(element));
}
return accumulator;
}
/* [...] */
enum Option<T> {
Some(T),
None
}
This might cause problems for readers who are typing in and trying out code that the tutorial provides (which is alluded to as a way of reading the tutorial in the current introduction). The reason is that creating an enum with some constructor that is the same as one of the constructors of the Option
type from the std module will create some weird shadowing-behaviour (see this issue). This kind of error is non-obvious, at least to a beginner, and the error message arises in the for
loop instead of the enum declaration, making things more confusing.
Replacing the Option
example with some other artificial (not found in std module) example would mitigate this risk, but on the other hand it would not give an excuse to introduce the Option
type and its role.