rust-lang / rust-lang/rfcs

derive(new)

Open
#1,318 14 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

The constructor pattern is well-used in Rust, but is very boiler-platey. I propose allowing #[derive(new)] to be added to structs to provide the common constructor pattern.

When placed on a struct called Foo with fields f_0 to f_n with types T_0 to T_n it would create an inherent impl for that struct with a single function called new which would take n args, with types T_0 to T_n and return a Foo, it would simply create a new Foo object mapping args to fields. It would be pub if Foo was pub. The impl would have the same type and lifetime parameters as the struct.

This is a bit different from existing derive functionallity, since it takes a function name rather than a trait name as input and creates an inherent impl, rather than a trait impl. But it would work in basically the same way and have all the same benefits.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files or tests are named. Start by reading the proposed #[derive(new)] behavior and the RFC process for Rust language changes; done means reaching a settled design decision about generated constructors, visibility, generics, lifetimes, and compatibility with existing derive behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.