Logo

Programming-Idioms

History of Idiom 247 > diff from v17 to v18

Edit summary for version 18 by programming-idioms.org:
[Rust] +DemoURL

Version 17

2021-03-09, 12:06:17

Version 18

2021-03-10, 09:28:59

Idiom #247 Filter list in-place

Remove all the elements from list x that don't satisfy the predicate p, without allocating a new list.
Keep all the elements that do satisfy p.

For languages that don't have mutable lists, refer to idiom #57 instead.

Idiom #247 Filter list in-place

Remove all the elements from list x that don't satisfy the predicate p, without allocating a new list.
Keep all the elements that do satisfy p.

For languages that don't have mutable lists, refer to idiom #57 instead.

Variables
x,p
Variables
x,p
Extra Keywords
keep conserve preserve mutable
Extra Keywords
keep conserve preserve mutable
Code
let mut x: Vec<T>;
x.retain(predicate);
Code
x.retain(p);
Comments bubble
The predicate p takes as argument a reference to the element.
Doc URL
https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain
Doc URL
https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0715b08629f206f36521c11fccdb2a5c