From bbd64b256320013d02734504c744cd3c0b180ebf Mon Sep 17 00:00:00 2001 From: Phlosioneer Date: Sat, 24 Feb 2018 11:37:38 -0500 Subject: [PATCH 1/3] 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/ --- src/libcore/option.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index b8fe28d0f0d71..96b551d8704ae 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -233,10 +233,10 @@ impl Option { /// [`usize`]: ../../std/primitive.usize.html /// /// ``` - /// let num_as_str: Option = Some("10".to_string()); + /// let num_as_str: Option = Some("12".to_string()); /// // First, cast `Option` to `Option<&String>` with `as_ref`, /// // then consume *that* with `map`, leaving `num_as_str` on the stack. - /// let num_as_int: Option = num_as_str.as_ref().map(|n| n.len()); + /// let num_length: Option = num_as_str.as_ref().map(|n| n.len()); /// println!("still can print num_as_str: {:?}", num_as_str); /// ``` #[inline] From e8904f935ab45a033c61e2136674a080046fd733 Mon Sep 17 00:00:00 2001 From: Phlosioneer Date: Sun, 25 Feb 2018 15:46:17 -0500 Subject: [PATCH 2/3] Change the example string to something arbitrary 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!". --- src/libcore/option.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 96b551d8704ae..5c9fe8600768e 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -233,11 +233,12 @@ impl Option { /// [`usize`]: ../../std/primitive.usize.html /// /// ``` - /// let num_as_str: Option = Some("12".to_string()); + /// let text: Option = Some("Hello, world!".to_string()); /// // First, cast `Option` to `Option<&String>` with `as_ref`, - /// // then consume *that* with `map`, leaving `num_as_str` on the stack. - /// let num_length: Option = num_as_str.as_ref().map(|n| n.len()); - /// println!("still can print num_as_str: {:?}", num_as_str); + /// // then consume *that* with `map`, leaving `text` on the stack. + /// let text_length: Option = text.as_ref().map(|s| s.len()); + /// println!("text length: {}", text_length); + /// println!("still can print text: {:?}", text); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] From b7b3498ce86d025ed84b0daa9a28588190ac444a Mon Sep 17 00:00:00 2001 From: Phlosioneer Date: Mon, 26 Feb 2018 19:48:15 -0500 Subject: [PATCH 3/3] Fix doctest failure Tried to be fancy with print statements. --- src/libcore/option.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 5c9fe8600768e..2b77ba3912279 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -237,7 +237,6 @@ impl Option { /// // First, cast `Option` to `Option<&String>` with `as_ref`, /// // then consume *that* with `map`, leaving `text` on the stack. /// let text_length: Option = text.as_ref().map(|s| s.len()); - /// println!("text length: {}", text_length); /// println!("still can print text: {:?}", text); /// ``` #[inline]