Skip to content

Commit 6f2100b

Browse files
committedMar 6, 2018
Auto merge of #48509 - Phlosioneer:option-doc-change, r=TimNN
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/
2 parents 1733a61 + b7b3498 commit 6f2100b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed
 

‎src/libcore/option.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ impl<T> Option<T> {
233233
/// [`usize`]: ../../std/primitive.usize.html
234234
///
235235
/// ```
236-
/// let num_as_str: Option<String> = Some("10".to_string());
236+
/// let text: Option<String> = Some("Hello, world!".to_string());
237237
/// // First, cast `Option<String>` to `Option<&String>` with `as_ref`,
238-
/// // then consume *that* with `map`, leaving `num_as_str` on the stack.
239-
/// let num_as_int: Option<usize> = num_as_str.as_ref().map(|n| n.len());
240-
/// println!("still can print num_as_str: {:?}", num_as_str);
238+
/// // then consume *that* with `map`, leaving `text` on the stack.
239+
/// let text_length: Option<usize> = text.as_ref().map(|s| s.len());
240+
/// println!("still can print text: {:?}", text);
241241
/// ```
242242
#[inline]
243243
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)