Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial: potential trip-up for beginners as a consequence of bug in for iteration #15409

Closed
LemmingAvalanche opened this issue Jul 4, 2014 · 5 comments

Comments

@LemmingAvalanche
Copy link
Contributor

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.

@steveklabnik
Copy link
Member

Yeah, I'm not sure what the right solution is here. Maybe put a comment above the enum saying "this is built-in, you dont need to declare it?"

@LemmingAvalanche
Copy link
Contributor Author

@steveklabnik But when some people follow tutorials they like to follow along by testing the code for themselves, even if the code is just a reimplementation of something that could be imported or used directly anyway. Maybe they just want to practice the syntax by typing in the examples and seeing if it compiles, for example.

@steveklabnik
Copy link
Member

Then maybe it should say "if you re-implement this, it will kinda work, but bad things will happen?"

@LemmingAvalanche
Copy link
Contributor Author

@steveklabnik That is a possibility, but it might not instil much confidence in the language. ^^ Though I guess that relatively early adopters have already mentally prepared themselves for rough edges, anyway.

A third possibility is to keep the enum name, but change the name of the constructors:

enum Option<T> { // or Maybe<T>... 
    Just(T),
    Nothing
}

This doesn't create an error.

A fourth possibility is to mangle the name of the constructors, i.e. Some1(T) and None1. Mildly distracting, but probably not a big deal.

@alexcrichton
Copy link
Member

This was fixed in the language by #15809

bors added a commit to rust-lang-ci/rust that referenced this issue Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants