Struct argmin::core::KV

source ·
pub struct KV {
    pub kv: HashMap<&'static str, KvValue>,
}
Expand description

A simple key-value storage

Keeps pairs of (&'static str, KvValue) and is used to pass key-value pairs to Observers in each iteration of an optimization algorithm. Typically constructed using the kv! macro.

Example

use argmin::kv;

let kv = kv!(
    "key1" => "value1";
    "key2" => "value2";
    "key3" => 1234;
);

Fields§

§kv: HashMap<&'static str, KvValue>

The actual key value storage

Implementations§

Constructor a new empty KV

Example
let kv = KV::new();

Insert a key-value pair

Example
let mut kv = KV::new();
kv.insert("key1", KvValue::Str("value".to_string()));
kv.insert("key2", KvValue::Int(1234));

Retrieve an element from the KV by key

Returns Some(<reference to KvValue>) if key is present and None otherwise.

Example
let mut kv1 = KV::new();
kv1.insert("key1", KvValue::Float(12.0));

assert_eq!(kv1.get("key1"), Some(&KvValue::Float(12.0)));
assert_eq!(kv1.get("non_existing"), None);

Returns all available keys and their KvValue kind

Example
let mut kv1 = KV::new();
kv1.insert("key1", KvValue::Str("value1".to_string()));

assert_eq!(kv1.keys(), vec![("key1", "Str")]);

Merge with another KV

Example
let mut kv1 = KV::new();
kv1.insert("key1", KvValue::Str("value1".to_string()));

let mut kv2 = KV::new();
kv2.insert("key2", KvValue::Str("value2".to_string()));

let kv1 = kv1.merge(kv2);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
Serialize self into Serializer Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.