Expand description
§Short Default
Avoid writing tedious Default implementations by using a simple default!
macro instead.
§Usage
use short_default::default;
// Define a new struct with default values
default! {
struct Config {
version: (u64, u64, u64) = (0, 1, 0),
// This default value will be inferred via
// authors: Default::default(),
authors: Vec<String>,
}
}
// Use the default implementation
let config = Config::default();
assert_eq!(config.version, (0, 1, 0));
assert_eq!(config.authors.len(), 0);§Side-by-Side Comparison
§Short Default
use short_default::default;
default! {
struct SomeOptions {
foo: i32 = 10,
bar: f32 = 5.0,
baz: String = "bazzz".to_string(),
qux: Option<bool> = Some(false),
corge: Vec<i32>,
}
}§Traditional Approach
struct SomeOptions {
foo: i32,
bar: f32,
baz: String,
qux: Option<bool>,
corge: Vec<i32>,
}
impl Default for SomeOptions {
fn default() -> Self {
Self {
foo: 10,
bar: 5.0,
baz: "bazz".to_string(),
qux: Some(false),
corge: Vec::default(),
}
}
}§Supported Features
The definition of the struct preceding the equal sign for each field is parsed conventionally and identically returned. This means that any regular syntax which such as field attributes, generics, etc. works as well.
Macros§
- default
- See the crate-level documentation