Skip to content

Confusing examples in Vec::extend_from_within #104762

Closed

Description

@HarrisonMc555

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]);

Activity

  1. added
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    on Nov 23, 2022
  2. HintringerFabian commented on Nov 23, 2022

    @HintringerFabian
    Contributor

    @rustbot claim

  3. removed their assignment
    on Jan 11, 2023
  4. LFS6502 commented on Dec 3, 2024

    @LFS6502
    Contributor

    @rustbot claim

  5. added 2 commits that reference this issue on Dec 6, 2024
    290a071
    b780404
  6. added a commit that references this issue on Dec 6, 2024
    0506b75
  7. added a commit that references this issue on Mar 11, 2025
    6d6cc17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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