Skip to content

binary_search docs should show how to insert something into an already-sorted list, while maintaining sort order #61684

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
thomcc opened this issue Jun 9, 2019 · 1 comment · Fixed by #63442
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@thomcc
Copy link
Member

thomcc commented Jun 9, 2019

This isn't exactly hard to do, but it's completely possible to have an off-by-one error because of overthinking it, and it's a common enough use case that it seems worth showing off in a doctest.

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jun 9, 2019
@NOVA-ME
Copy link

NOVA-ME commented Jun 10, 2019

fn main() {
    let nums = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
    // Any number is ok, but must be i32.
    let insert_num = 14;
    // Find the index
    let index = match nums.binary_search(&insert_num) {
        Ok(a) => a,
        Err(b) => b
    };
    let mut insert_nums: [i32; 14] = [0; 14];
    // Insert into a new array.
    for i in 0..14 {
        if i > index {
            insert_nums[i] = nums[i - 1];
        } else if i < index {
            insert_nums[i] = nums[i];
        } else {
            insert_nums[i] = insert_num;
        }
    }
    println!("{:?}", insert_nums);
    assert_eq!([-100, 0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55], insert_nums);
}

I'm a noob in rust, this is my solution. ❤️

ptallo pushed a commit to ptallo/rust that referenced this issue Jun 12, 2019
…ed list

Resolves: rust-lang#61684

Apply suggestions from code review

Co-Authored-By: Jonas Schievink <jonasschievink@gmail.com>

Cleanup: Fixed failing tidy checks
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Aug 11, 2019
…k-Simulacrum

Add an example to show how to insert item to a sorted vec

Closes rust-lang#61684
cc rust-lang#61742
r? @Mark-Simulacrum, @jonas-schievink
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Aug 11, 2019
…k-Simulacrum

Add an example to show how to insert item to a sorted vec

Closes rust-lang#61684
cc rust-lang#61742
r? @Mark-Simulacrum, @jonas-schievink
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
3 participants