pub struct Map { /* private fields */ }Expand description
A predicator map: Keys, each with a Value, kept in key order.
use predicator_rs::{Key, Map, Value};
let mut map = Map::new();
map.insert("b", Value::Integer(2));
map.insert("a", Value::Integer(1));
map.insert(true, Value::Null);
assert_eq!(map.len(), 3);
assert_eq!(map.get(&"a".into()), Some(&Value::Integer(1)));
assert_eq!(map.get(&Key::Integer(1)), None);
assert_eq!(
map.keys().collect::<Vec<_>>(),
[&Key::from(true), &Key::from("a"), &Key::from("b")]
);Implementations§
Source§impl Map
impl Map
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
How many keys the map holds.
use predicator_rs::{Map, Value};
let map: Map = [("a", Value::Null), ("b", Value::Null)].into_iter().collect();
assert_eq!(map.len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the map holds no keys.
use predicator_rs::{Map, Value};
let mut map = Map::new();
assert!(map.is_empty());
map.insert("a", Value::Null);
assert!(!map.is_empty());Sourcepub fn get(&self, key: &Key) -> Option<&Value>
pub fn get(&self, key: &Key) -> Option<&Value>
The value at key, if the map holds it.
use predicator_rs::{Key, Map, Value};
let map: Map = [(1, Value::from("x"))].into_iter().collect();
assert_eq!(map.get(&Key::from(1)), Some(&Value::from("x")));
assert_eq!(map.get(&Key::from("1")), None);Sourcepub fn get_mut(&mut self, key: &Key) -> Option<&mut Value>
pub fn get_mut(&mut self, key: &Key) -> Option<&mut Value>
The value at key, mutably, if the map holds it.
use predicator_rs::{Key, Map, Value};
let mut map: Map = [("a", Value::from(1))].into_iter().collect();
if let Some(value) = map.get_mut(&Key::from("a")) {
*value = Value::from(2);
}
assert_eq!(map.get(&Key::from("a")), Some(&Value::from(2)));Sourcepub fn contains_key(&self, key: &Key) -> bool
pub fn contains_key(&self, key: &Key) -> bool
Whether the map holds key.
use predicator_rs::{Key, Map, Value};
let map: Map = [(true, Value::Null)].into_iter().collect();
assert!(map.contains_key(&Key::from(true)));
assert!(!map.contains_key(&Key::from(false)));Sourcepub fn insert(&mut self, key: impl Into<Key>, value: Value) -> Option<Value>
pub fn insert(&mut self, key: impl Into<Key>, value: Value) -> Option<Value>
Puts value at key, answering the value it replaces.
use predicator_rs::{Map, Value};
let mut map = Map::new();
assert_eq!(map.insert("a", Value::from(1)), None);
assert_eq!(map.insert("a", Value::from(2)), Some(Value::from(1)));Sourcepub fn remove(&mut self, key: &Key) -> Option<Value>
pub fn remove(&mut self, key: &Key) -> Option<Value>
Takes the value at key out of the map.
use predicator_rs::{Key, Map, Value};
let mut map: Map = [("a", Value::from(1))].into_iter().collect();
assert_eq!(map.remove(&Key::from("a")), Some(Value::from(1)));
assert_eq!(map.remove(&Key::from("a")), None);Sourcepub fn iter(&self) -> impl Iterator<Item = (&Key, &Value)>
pub fn iter(&self) -> impl Iterator<Item = (&Key, &Value)>
The keys and values, in key order.
use predicator_rs::{Key, Map, Value};
let map: Map = [("b", Value::from(2)), ("a", Value::from(1))].into_iter().collect();
let pairs: Vec<_> = map.iter().collect();
assert_eq!(pairs[0], (&Key::from("a"), &Value::from(1)));Trait Implementations§
Source§impl IntoIterator for Map
impl IntoIterator for Map
impl StructuralPartialEq for Map
Auto Trait Implementations§
impl Forget for Map
impl Freeze for Map
impl Move for Map
impl RefUnwindSafe for Map
impl Send for Map
impl Sync for Map
impl Unpin for Map
impl UnsafeUnpin for Map
impl UnwindSafe for Map
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more