default_is_triple_underscore

Crates.iodefault_is_triple_underscore
lib.rsdefault_is_triple_underscore
version0.2.0
sourcesrc
created_at2024-07-05 09:21:06.568306
updated_at2024-08-05 23:08:14.203556
descriptionShorter syntax for Default::default() : ___()
homepagehttps://github.com/Thomas-Mewily/triple_underscore_for_default
repositoryhttps://github.com/Thomas-Mewily/triple_underscore_for_default
max_upload_size
id1292554
size4,784
Thomas Mewily (Thomas-Mewily)

documentation

https://docs.rs/default_is_triple_underscore

README

A shorter way to write default. Provide :

  • ___() as a shorthand for Default::default()
  • i32::___() instead of i32::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::___());
Commit count: 3

cargo fmt