dedup function not returning the expected output

Viewed 69

I'm trying to eliminate duplicates in a sorted vector with the dedup function, but it returns empty.

Here's my code:

fn main() {
    let mut input = vec![1, 1, 2, 3, 3];
    remove_dup(&mut input);
}

fn remove_dup(input: &mut Vec<u32>) { 
    let result = input.dedup(); 
    println!("Result: {:?}", result);
}

Here's the output:

Result: () 
1 Answers

The dedup() method operates in-place, updating the original vector.

fn main() {
    let mut input = vec![1, 1, 2, 3, 3];
    remove_dup(&mut input);
}

fn remove_dup(input: &mut Vec<u32>) { 
    input.dedup(); 
    println!("Result: {:?}", input);
}

Playground.

In general, when you see a function that returns () (no return type), it's a good sign that it's a modifying operation.

Related