📜  remove on condtion in vec rust - C 编程语言(1)

📅  最后修改于: 2023-12-03 15:04:53.146000             🧑  作者: Mango

Remove on Condition in vec Rust

In Rust programming language, Vec is a dynamically-sized array. To remove an element from the vector, you can use the remove() method, which removes the element at the specified index and returns it. However, sometimes you may want to remove elements based on a certain condition.

In this article, we'll explore how to remove elements from a vector based on a condition using different ways in Rust programming language.

Remove elements based on condition using drain_filter() method:

The drain_filter() method was introduced in Rust 1.21. It removes all elements from the vector that match a given predicate, and returns an iterator over the removed items.

fn main() {
    let mut nums = vec![1, 2, 3, 4, 5];
    nums.drain_filter(|x| *x % 2 == 0).collect::<Vec<_>>();
    println!("{:?}", nums);
}

Output: [1, 3, 5]

In the above example, we create a vector nums with some integers. We then call the drain_filter() method on nums, with a closure that checks if an element is even or not.

The drain_filter() method returns an iterator over the removed elements, in this case, the even numbers. We use the collect() method on this iterator to collect the removed items into a new vector.

Finally, we print the modified vector nums without even numbers.

Remove elements based on condition using retain() method:

The retain() method is another way to remove elements from a vector based on a condition. This method modifies the vector in place and keeps only the elements that satisfy the condition.

fn main() {
    let mut nums = vec![1, 2, 3, 4, 5];
    nums.retain(|x| *x % 2 != 0);
    println!("{:?}", nums);
}

Output: [1, 3, 5]

In the above example, we create a vector nums with some integers. We then call the retain() method on nums, with a closure that checks if an element is odd or not.

The retain() method keeps only the elements for which the closure returns true. In this case, the closure returns true if an element is odd.

Finally, we print the modified vector nums without even numbers.

Remove elements based on condition using filter() method:

The filter() method is the most basic way to remove elements from a vector based on a condition. This method returns a new iterator over the vector's contents, which satisfies the condition.

fn main() {
    let nums = vec![1, 2, 3, 4, 5];
    let new_vec: Vec<i32> = nums.iter().filter(|x| *x % 2 != 0).map(|x| *x).collect();
    println!("{:?}", new_vec);
}

Output: [1, 3, 5]

In the above example, we create a vector nums with some integers. We then call the filter() method on nums, with a closure that checks if an element is odd or not.

The filter() method returns a new iterator over the vector's contents that the closure returns true for. In this case, the closure returns true if an element is odd.

We use the map() method on the iterator to shift the ownership to the elements, and finally, we collect the filtered items into a new vector.

Finally, we print the new vector new_vec without even numbers.

Conclusion

In this article, we learned three different ways to remove elements from a vector based on a condition in Rust. Each method has its own use case, depending on your requirement. The drain_filter() method is the most powerful way to remove items from a vector based on a condition, while retain() method modifies the vector in place. The filter() method is the most basic way to remove items from a vector by returning a new iterator over the vector's contents.