Getting a Default value in Rust

I write a lot of Rust code, and one things that annoy me is that you need a lot of keyboard press to get the default value of a structure/enum using the Default trait , and it take a lot of space on the screen.

let x = i32::default(); // use the type
/* ... */

You can also use type deduction :

fn bar(x : i32) { /* ... */ }

// in main :
bar(Default::default()); // it know that the type is i32

Long code can be found when initiliazing a struct :

struct Player
{
    hp  : i32,

    lvl    : i32,
    damage : i32,
    invisible : bool,
    gold_coin : i32,
}

// in main :
// it take so much space on screen...
let p = Player { hp: 100, lvl: Default::default(), damage: Default::default(), invisible: Default::default(), gold_coin: Default::default() };

Here I don’t use the #[derive(Default)] macro because the default hp should be 100, only the rest use the default value.

Less typing and screen space wasted

So I was tired of those really long line, so I made this crate, Default is triple underscore . This crate is the manifestation of the lazy developer that I am to write less characters, and keep the code more easily readable.

It define a ___() global function (using type inference), and a i32::___() function for each type that implement the Default trait.

The same code then becomes:

let x = i32::___();
fn bar(x : i32) { /* ... */ }

bar(___());

And most notably :

let p = Player { hp: 100, lvl: Default::default(), damage: Default::default(), invisible: Default::default(), gold_coin: Default::default() };
// =>
let p = Player { hp: 100, lvl: ___(), damage: ___(), invisible: ___(), gold_coin: ___() };

Of course, I can put the direct value in this case…

let p = Player { hp: 100, lvl: 0, damage: 0, ininvisible: false, gold_coin: 0 };

…but sometime I work with more complexe type like HashMap, Vec… and typing ___() is quicker and take less space than HashMap::new().

This crate is also the first one that I shared on crate io, so yep I don’t do a lot of stuff, but it is somewhat useful I guess ?!

Code Source

The source code for the crate is just :

pub fn ___<T:Default>() -> T {
    std::default::Default::default()
}

pub trait DefaultIsTripleUnderscore : Default
{
    fn ___() -> Self { Self::default() }
}

impl<T:Default> DefaultIsTripleUnderscore for T {}

You can add the crate with the cargo command cargo add default_is_triple_underscore or by adding this line to your toml default_is_triple_underscore = "0.2.0"

Some Link

This is also my first post, that why it is “simple” and not too technical.