4 releases
0.2.0 | Aug 5, 2024 |
---|---|
0.1.2 | Aug 5, 2024 |
0.1.1 | Jul 5, 2024 |
0.1.0 | Jul 5, 2024 |
#25 in #initialization
139 downloads per month
5KB
A shorter way to write default. Provide :
___()
as a shorthand forDefault::default()
i32::___()
instead ofi32::default()
Based on the internals Rust discussion
Also check the Defaults crate which use default()
instead of Default::default()
Examples
let b : i32 = Default::default(); // Default Rust
let a : i32 = ___(); // Now
assert_eq!(a, b);
Can also be used with function :
let a = f(Default::default()); // Default Rust
let b = f(___()); // Now
assert_eq!(a, b);
Can also be used to initialize complex Rust struct when implementing the Default
trait :
impl Default for ComplexeStruct {
fn default() -> Self {
Self { a : ___(), b : ___(), c : ___(), vec : vec![0] }
// instead of
// Self { a : Default::default(), b : Default::default(), c : Default::default(), vec : vec![0] }
}
}
Uniform syntax : MyStruct::___()
instead of MyStruct::default()
:
type T = i32; // any type with default
assert_eq!(T::default(), T::___());