nushell/crates/nu-protocol/src/value
Thomas Coratger 0bd4d27e8d
Modify reject algorithm for identical elements (#8446)
# Description

The correction made here concerns the issue #8431. Indeed, the algorithm
initially proposed to remove elements of a `vector` performed a loop
with `remove` and an incident therefore appeared when several values
were equal because the deletion was done outside the length of the
vector:
```rust
let mut found = false;
for (i, col) in cols.clone().iter().enumerate() {
    if col == col_name {
        cols.remove(i);
        vals.remove(i);
        found = true;
    }
}

```

Then, `[[a, a]; [1, 2]] | reject a: ` gave `thread 'main' panicked at
'removal index (is 1) should be < len (is 1)',
crates/nu-protocol/src/value/mod.rs:1213:54`.

The proposed correction is therefore the implementation of the
`retain_mut` utility dedicated to this functionality.

```rust
let mut found = false;
let mut index = 0;
cols.retain_mut(|col| {
    if col == col_name {
        found = true;
        vals.remove(index);
        false
    } else {
        index += 1;
        true
    }
});
```
2023-03-14 23:26:48 +01:00
..
custom_value.rs Fix to json for SQLite databases (#8343) 2023-03-06 14:36:26 -08:00
from.rs Document and critically review ShellError variants - Ep. 3 (#8340) 2023-03-06 18:33:09 +01:00
from_value.rs Document and critically review ShellError variants - Ep. 3 (#8340) 2023-03-06 18:33:09 +01:00
lazy_record.rs LazyRecord (#7619) 2023-01-18 19:27:26 -08:00
mod.rs Modify reject algorithm for identical elements (#8446) 2023-03-14 23:26:48 +01:00
range.rs Box ShellError in Value::Error (#8375) 2023-03-12 09:57:27 +01:00
stream.rs Check ctrl+c when collecting a RawStream (#8020) 2023-02-10 20:23:46 +13:00
unit.rs Remove month and year duration constants (#6613) 2022-09-26 09:55:13 +13:00