Crates.io | entry_put_ext |
lib.rs | entry_put_ext |
version | 0.1.0 |
source | src |
created_at | 2023-11-11 21:06:59.564973 |
updated_at | 2023-11-11 21:06:59.564973 |
description | Map entry extension for put operations. |
homepage | |
repository | https://github.com/nossie531/entry_put_ext |
max_upload_size | |
id | 1032427 |
size | 18,923 |
Map entry extension for put operations.
The author of this crate is not good at English.
Forgive me if the document is hard to read.
This crate extends result type of the entry
methods of the maps (HashMap
and BTreeMap
) by adding an put
methods. These put
methods are helper
method to set a value of an entry and get a reference to it. This makes
common code about setting and getting shortened a little.
use std::collections::HashMap;
use entry_put_ext::hash_map::EntryPutExt;
let mut map = HashMap::from([("X", false)]);
let x = *map.entry("X").put(true);
let y = *map.entry("Y").put(true);
assert_eq!(x, map["X"]);
assert_eq!(y, map["Y"]);
Without this crate.
insert
and get
(Simple, but little slow and not one-liner).
use std::collections::HashMap;
let mut map = HashMap::from([("X", false)]);
map.insert("X", true);
map.insert("Y", true);
let x = *map.get("X").unwrap();
let y = *map.get("Y").unwrap();
assert_eq!(x, map["X"]);
assert_eq!(y, map["Y"]);
and_modify
and or_insert
(required Copy
trait for value type).
use std::collections::HashMap;
let mut map = HashMap::from([("X", false)]);
let (xv, yv) = (true, true);
let x = *map.entry("X").and_modify(|x| *x = xv).or_insert(xv);
let y = *map.entry("Y").and_modify(|x| *x = yv).or_insert(yv);
assert_eq!(x, map["X"]);
assert_eq!(y, map["Y"]);