auto-default

Crates.ioauto-default
lib.rsauto-default
version0.2.1
created_at2026-01-13 21:29:31.055317+00
updated_at2026-01-14 00:46:58.241285+00
descriptionMacro that adds a default field value of `Default::default()` to fields that don't have one
homepage
repositoryhttps://github.com/nik-rev/auto-default
max_upload_size
id2041400
size68,919
Nik Revenco (nik-rev)

documentation

README

auto-default

crates.io docs.rs license msrv github

This crate provides an attribute macro #[auto_default], which adds a default field value of Default::default() to fields that do not have one.

[dependencies]
auto-default = "0.2"

Note: auto-default has zero dependencies. Not even syn! The compile times are very fast.

Showcase

Rust's default field values allow the shorthand Struct { field, .. } instead of the lengthy Struct { field, ..Default::default() }

For .. instead of ..Default::default() to work, your Struct needs all fields to have a default value.

This often means = Default::default() boilerplate on every field, because it is very common to want field defaults to be the value of their Default implementation

Before

#[derive(Default)]
pub struct Layout {
    order: u32 = Default::default(),
    location: Point = Default::default(),
    size: Size = Default::default(),
    content_size: Size = Default::default(),
    scrollbar_size: Size = Default::default(),
    border: Rect = Default::default(),
    padding: Rect = Default::default(),
    margin: Rect = Default::default(),
}

With #[auto_default]

#[auto_default]
#[derive(Default)]
pub struct Layout {
    order: u32,
    location: Point,
    size: Size,
    content_size: Size,
    scrollbar_size: Size,
    border: Rect,
    padding: Rect,
    margin: Rect,
}

You can apply the #[auto_default] macro to structs with named fields, and enums.

If any field or variant has the #[auto_default(skip)] attribute, a default field value of Default::default() will not be added

Commit count: 39

cargo fmt