百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

derive-new

> 编程语言
开源

为 Rust 结构体衍生简单的构造函数

573 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

为 Rust 结构体衍生简单的构造函数

A custom derive implementation for #[derive(new)]

A derive(new) attribute creates a new constructor function for the annotated type. That function takes an argument for each field in the type giving a trivial constructor. This is useful since as your type evolves you can make the constructor non-trivial (and add or remove fields) without changing client code (i.e., without breaking backwards compatibility). It is also the most succinct way to initialise a struct or an enum.

Implementation uses macros 1.1 custom derive (which works in stable Rust from 1.15 onwards).

#[no_std] is fully supported if you switch off the default feature "std".

Examples

Cargo.toml:

toml
[dependencies]
derive-new = "0.5"

Include the macro:

  • Rust Edition 2015

    rust
    #[macro_use]
    extern crate derive_new;
  • Rust Edition 2018

    rust
    use derive_new::new;

Generating constructor for a simple struct:

rust
#[derive(new)]
struct Bar {
    a: i32,
    b: String,
}

let _ = Bar::new(42, "Hello".to_owned());

Default values can be specified either via #[new(default)] attribute which removes the argument from the constructor and populates the field with Default::default(), or via #[new(value = "..")] which initializes the field with a given expression:

rust
#[derive(new)]
struct Foo {
    x: bool,
    #[new(value = "42")]
    y: i32,
    #[new(default)]
    z: Vec,
}

let _ = Foo::new(true);

To make type conversion easier, #[new(into)] attribute changes the parameter type to impl Into, and populates the field with value.into():

rust
#[derive(new)]
struct Foo {
    #[new(into)]
    x: String,
}

let _ = Foo::new("Hello");

For iterators/collections, #[new(into_iter = "T")] attribute changes the parameter type to impl IntoIterator, and populates the field with value.into_iter().collect():

rust
#[derive(new)]
struct Foo {
    #[new(into_iter = "bool")]
    x: Vec,
}

let _ = Foo::new([true, false]);
let _ = Foo::new(Some(true));

Generic types are supported; in particular, PhantomData fields will be not included in the argument list and will be initialized automatically:

rust
use std::marker::PhantomData;

#[derive(new)]
struct Generic {
    x: &'a str,
    y: PhantomData
,
    #[new(default)]
    z: T,
}

let _ = Generic::::new("Hello");

For enums, one constructor method is generated for each variant, with the type name being converted to snake case; otherwise, all features supported for structs work for enum variants as well:

rust
#[derive(new)]
enum Enum {
    FirstVariant,
    SecondVariant(bool, #[new(default)] u8),
    ThirdVariant { x: i32, #[new(value = "vec![1]")] y: Vec }
}

let _ = Enum::new_first_variant();
let _ = Enum::new_second_variant(true);
let _ = Enum::new_third_variant(42);

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言