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

More clarity in the example for VecDeque.get #106114

Closed
amarao opened this issue Dec 24, 2022 · 3 comments · Fixed by #106562
Closed

More clarity in the example for VecDeque.get #106114

amarao opened this issue Dec 24, 2022 · 3 comments · Fixed by #106562
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools

Comments

@amarao
Copy link

amarao commented Dec 24, 2022

Location

https://doc.rust-lang.org/std/collections/struct.VecDeque.html#method.get

Summary

There is a bit of ambiguity about 'front' and 'back' thing in VecDeque, and documentation example fuel this uncertainty a bit:

let mut buf = VecDeque::new();
buf.push_back(3);
buf.push_back(4);
buf.push_back(5);
assert_eq!(buf.get(1), Some(&4));

Is get counts from top or from bottom? Basically, from this example it's hard to get simple answer, what buf.get(2) would be, 3 or 5?

I understand, that deeper thinking may lead to a proper conclusion, but, by adding one more line buf.push_back(6); can clarify it without causing any doubts.

My proposal is to change it to

let mut buf = VecDeque::new();
buf.push_back(3);
buf.push_back(4);
buf.push_back(5);
buf.push_back(6);
assert_eq!(buf.get(1), Some(&4));

In this example there is only one way to interpret 'offset 1' without any ambiguity.

@amarao amarao added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Dec 24, 2022
@clubby789
Copy link
Contributor

The documentation for that method specifies Element at index 0 is the front of the queue., which is pretty explicit

@amarao
Copy link
Author

amarao commented Jan 5, 2023

Yes, but explicit example without false symmetry will make it easier to read. What's the point for an example to provide ambiguity?

JohnTitor pushed a commit to JohnTitor/rust that referenced this issue Jan 8, 2023
…k-Simulacrum

Clarify examples for `VecDeque::get/get_mut`

Closes rust-lang#106114

`@rustbot` label +A-docs
@bors bors closed this as completed in 14ce603 Jan 8, 2023
@amarao
Copy link
Author

amarao commented Jan 8, 2023

Thank you!

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.

2 participants