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

Confusing examples in Vec::extend_from_within #104762

Closed
HarrisonMc555 opened this issue Nov 23, 2022 · 2 comments · Fixed by #133790
Closed

Confusing examples in Vec::extend_from_within #104762

HarrisonMc555 opened this issue Nov 23, 2022 · 2 comments · Fixed by #133790
Assignees
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools

Comments

@HarrisonMc555
Copy link

HarrisonMc555 commented Nov 23, 2022

Location

Vec::extend_from_within

Summary

The examples are confusing because they use increasing integer sequences as both the vector elements and the range. This leas to a confusing example where you call vec.extend_from_within(2..); and add 2..4 to the end of the vector. When I first read this, I assumed that it was adding the elements of the range to the end of the vector and that the vector had only been initialized with enough "extra room" for three more elements. It took me a while to realize that the range was specifying a range inside the vector and then appending it to the end. Looking back, the name extend_from_within should have been a pretty good hint. However, every example adds numbers to the end that matches the first number of the provided range.

let mut vec = vec![0, 1, 2, 3, 4];

// Adds 2..4 to end of vector
vec.extend_from_within(2..);
assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);

// Adds 0..1 to end of vector
vec.extend_from_within(..2);
assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);

// Adds 4 (and then 2..4) to end of vector
vec.extend_from_within(4..8);
assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);

If you think about the name of the method and the description of the method, you can figure out what this function does. However, the examples can be fairly misleading. I believe this can be rectified by using non-integer values in the vector. For example:

let mut vec = vec!['a', 'b', 'c'];

vec.extend_from_within(1..);
assert_eq!(vec, ['a', 'b', 'c', 'b', 'c']);

vec.extend_from_within(..1);
assert_eq!(vec, ['a', 'b', 'c', 'b', 'c', 'a', 'b']);

vec.extend_from_within(2..5);
assert_eq!(vec, ['a', 'b', 'c', 'b', 'c', 'a', 'b', 'c', 'b', 'c', 'a']);

Do others agree that this is an improvement? I know a lot of the other examples use integers, so maybe it's not worth making this example different. At the very least, we could improve it by using different numbers instead 0, 1, 2, etc.

let mut vec = vec![31, 37, 41, 43];

vec.extend_from_within(1..);
assert_eq!(vec, [31, 37, 41, 43, 37, 41, 43]);

vec.extend_from_within(..2);
assert_eq!(vec, [31, 37, 41, 43, 37, 41, 43, 31, 37]);

vec.extend_from_within(2..5);
assert_eq!(vec, [31, 37, 41, 43, 37, 41, 43, 31, 37, 41, 43, 37]);
@HarrisonMc555 HarrisonMc555 added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Nov 23, 2022
@HintringerFabian
Copy link
Contributor

@rustbot claim

@HypheX
Copy link
Contributor

HypheX commented Dec 3, 2024

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 6, 2024
…ondi,workingjubilee

Improve documentation for Vec::extend_from_within

This closes rust-lang#104762.

It rephrases some of the explanations, and greatly improves the clarity of the example.
Based on this PR and its discussions:
https://github.com/rust-lang/rust/pull/105030/files#r1059808792
@bors bors closed this as completed in b780404 Dec 6, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 6, 2024
Rollup merge of rust-lang#133790 - HypheX:improve-vec-docs, r=harudagondi,workingjubilee

Improve documentation for Vec::extend_from_within

This closes rust-lang#104762.

It rephrases some of the explanations, and greatly improves the clarity of the example.
Based on this PR and its discussions:
https://github.com/rust-lang/rust/pull/105030/files#r1059808792
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
3 participants