Expand description
§anyval
A lightweight, dynamically‑typed value container for Rust.
anyval provides Map, Array, and Value types that let you store heterogeneous data (numbers, strings, booleans, nested maps/arrays) with a simple, ergonomic API. It’s ideal for configuration handling, scripting, or any situation where you need a flexible data model without pulling in a full‑blown JSON library.
§Features
- Dynamic typing –
Valuecan hold floats, ints, bools, strings, maps, arrays, orNone. - Zero‑allocation on creation – containers allocate only when needed.
- Serde support – optional
serdeintegration for (de)serialization. - Convenient macros –
map!andarray!for quick literal construction. - Optional JSON helpers –
to_json/from_jsonbehind thejsonfeature.
§Quick start
# Add anyval with the desired features
cargo add anyval
# Add anyval without the default features (no json & serde)
cargo add anyval --no-default-featuresuse anyval::{map, array, Value};
fn main() {
// Build a map with mixed types
let cfg = map! {
"host" => "localhost",
"port" => 8080,
"debug" => true,
"paths" => array!["/api", "/static"],
};
// Access values
println!("Host: {}", cfg["host"].as_str());
println!("Port: {}", cfg["port"].as_int());
// Serialize to JSON (requires the `json` feature)
#[cfg(feature = "json")]
println!("JSON: {}", cfg.to_json().unwrap());
}§Documentation
For full API details, examples, and feature flags, see the crate documentation:
Macros§
- array
- Creates an
Arraycontaining the given values. - map
- Creates a
Mapcontaining the given key-value pairs.
Structs§
- Array
- A dynamically‑sized array that stores
Valueinstances. - Map
- A map of string keys to dynamically typed values.
Enums§
- Str
- A string type that can hold either a
'staticstring slice, anArc<String>for cheap cloning, or an ownedString. - Value
- A dynamically‑typed value used throughout the library.
- Value
Ref - A borrowed reference to a
Value. - Value
RefMut - A mutable borrowed reference to a
Value.