Skip to content

More clarity in the example for VecDeque.get #106114

Closed
@amarao

Description

@amarao

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions