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

Option.as_ref() possible example mistake? #104476

Closed
remdragon opened this issue Nov 16, 2022 · 7 comments · Fixed by #104965
Closed

Option.as_ref() possible example mistake? #104476

remdragon opened this issue Nov 16, 2022 · 7 comments · Fixed by #104965
Assignees
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools

Comments

@remdragon
Copy link

remdragon commented Nov 16, 2022

url: https://doc.rust-lang.org/nightly/core/option/enum.Option.html

function: as_ref()

Examples:

Converts an Option<String> into an Option<usize>, preserving the original.

Perhaps I'm just confused, but shouldn't that say:

Converts an Option<String> into an Option<&str>, preserving the original.

@ehuss ehuss transferred this issue from rust-lang/reference Nov 16, 2022
@ehuss
Copy link
Contributor

ehuss commented Nov 16, 2022

Transferring to the rust-lang/rust repository, as that is where the standard library documentation is.

I think it is trying to illustrate the conversion from Option<String>Option<&String>Option<usize>. The text is just eliding the middle step, and explaining where it is starting from and where it is finishing.

@ehuss ehuss added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Nov 16, 2022
@aristotaloss
Copy link

No, that seems fine; what confuses you (or leads you to the &str conclusion)? I could follow Option<&String> but not Option<&str>. Perhaps you're thinking the Deref<Target=str> impl on String would make the Option contain Option<&str> or such?

@remdragon
Copy link
Author

remdragon commented Nov 16, 2022

I guess I'm extremely confused then. If I have an &Option<String>, what good does an Option<usize> do me? If I have &Some( "fubar" ), how does that translate to a usize? Is it giving me the length of the string or it's pointer in memory? Neither seems useful nor relevant to the description of "Converts from &Option to Option<&T>.".

@zirconium-n
Copy link
Contributor

I guess I'm extremely confused then. If I have an &Option, what good does an Option do me?

You can call Option::map.

If I have &Some( "fubar" ), how does that translate to a usize? Is it giving me the length of the string or it's pointer in memory? Neither seems useful nor relevant to the description of "Converts from &Option to Option<&T>.".

Converts an Option<String> into an Option<usize>, preserving the original just describes the example code's functionality, not Option::as_ref's. It's showing one of the ways you could use Option::as_ref.

@remdragon
Copy link
Author

Okay, sorry, I understand now that comment is talking about the upcoming example instead of discussing as_ref() in general. In order to help others not experience the same confusion I did, perhaps I could suggest a tweak to the wording, such as: Calculates the length of an Option<String> as an Option<usize> without consuming the original String.?

@M1ngXU
Copy link

M1ngXU commented Nov 17, 2022

length

memory address

@zacklukem
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants