-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Slight modification to the as_ref example of std::option::Option #48509
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
Conversation
A user in a reddit thread was confused by the name of the variable "num_as_int"; they thought the example was trying to convert the string "10" as if it were binary 2 by calling str::len(). In reality, the example is simply demonstrating how to take an immutable reference to the value of an Option. The confusion comes from the coincidence that the length of the string "10" is also its binary representation, and the implication from the variable names that a conversion was occuring ("num_as_str" to "num_as_int"). This PR changes the example number to 12 instead of 10, and changes the variable name from "num_as_int" to "num_length" to better communicate what the example is doing. The reddit thread: https://www.reddit.com/r/rust/comments/7zpvev/notyetawesome_rust_what_use_cases_would_you_like/dur39xw/
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @TimNN (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
src/libcore/option.rs
Outdated
@@ -233,10 +233,10 @@ impl<T> Option<T> { | |||
/// [`usize`]: ../../std/primitive.usize.html | |||
/// | |||
/// ``` | |||
/// let num_as_str: Option<String> = Some("10".to_string()); | |||
/// let num_as_str: Option<String> = Some("12".to_string()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To address the problem more directly it might make sense to change this completely -- the fact that the string contents are numbers is irrelevant. Maybe instead
let text: Option<String> = Some("Hello, World!".to_string());
let text_length: Option<usize> = text.as_ref().map(|t| t.len());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea. I'll change that.
The choice of string is arbitrary, so all references to a number in the string were removed. The string is now the standard "Hello world!".
Tried to be fancy with print statements.
@neandrake I fixed what you commented on, could you review this again when you get a chance? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea it looks good to me. I'm not sure if I can approve though I'm not part of the project.
Who needs to review this, then? We should probably |
@TimNN Could you review this / merge it? Or point me to someone who can? |
@bors r+ rollup. Thanks for the PR, @Phlosioneer! Sorry for the delay in reviewing. |
📌 Commit b7b3498 has been approved by |
Slight modification to the as_ref example of std::option::Option A user in a reddit thread was confused by the name of the variable "num_as_int"; they thought the example was trying to convert the string "10" as if it were binary 2 by calling str::len(). In reality, the example is simply demonstrating how to take an immutable reference to the value of an Option. The confusion comes from the coincidence that the length of the string "10" is also its binary representation, and the implication from the variable names that a conversion was occuring ("num_as_str" to "num_as_int"). This PR changes the example number to 12 instead of 10, and changes the variable name from "num_as_int" to "num_length" to better communicate what the example is doing. The reddit thread: https://www.reddit.com/r/rust/comments/7zpvev/notyetawesome_rust_what_use_cases_would_you_like/dur39xw/
☀️ Test successful - status-appveyor, status-travis |
A user in a reddit thread was confused by the name of the variable
"num_as_int"; they thought the example was trying to convert the
string "10" as if it were binary 2 by calling str::len(). In reality,
the example is simply demonstrating how to take an immutable reference
to the value of an Option. The confusion comes from the coincidence
that the length of the string "10" is also its binary representation,
and the implication from the variable names that a conversion was
occuring ("num_as_str" to "num_as_int").
This PR changes the example number to 12 instead of 10, and changes
the variable name from "num_as_int" to "num_length" to better
communicate what the example is doing.
The reddit thread:
https://www.reddit.com/r/rust/comments/7zpvev/notyetawesome_rust_what_use_cases_would_you_like/dur39xw/