{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rfcs/issues/1318","labels_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1318/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1318/comments","events_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1318/events","html_url":"https://github.com/rust-lang/rfcs/issues/1318","id":111120792,"number":1318,"title":"derive(new)","user":{"login":"nrc","id":762626,"avatar_url":"https://avatars.githubusercontent.com/u/762626?v=3","gravatar_id":"","url":"https://api.github.com/users/nrc","html_url":"https://github.com/nrc","followers_url":"https://api.github.com/users/nrc/followers","following_url":"https://api.github.com/users/nrc/following{/other_user}","gists_url":"https://api.github.com/users/nrc/gists{/gist_id}","starred_url":"https://api.github.com/users/nrc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nrc/subscriptions","organizations_url":"https://api.github.com/users/nrc/orgs","repos_url":"https://api.github.com/users/nrc/repos","events_url":"https://api.github.com/users/nrc/events{/privacy}","received_events_url":"https://api.github.com/users/nrc/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-10-13T07:25:30Z","updated_at":"2015-10-13T08:51:48Z","closed_at":null,"body":"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.\r\n\r\nWhen 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.\r\n\r\nThis 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."},"comment":{"url":"https://api.github.com/repos/rust-lang/rfcs/issues/comments/147651854","html_url":"https://github.com/rust-lang/rfcs/issues/1318#issuecomment-147651854","issue_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1318","id":147651854,"user":{"login":"nagisa","id":679122,"avatar_url":"https://avatars.githubusercontent.com/u/679122?v=3","gravatar_id":"","url":"https://api.github.com/users/nagisa","html_url":"https://github.com/nagisa","followers_url":"https://api.github.com/users/nagisa/followers","following_url":"https://api.github.com/users/nagisa/following{/other_user}","gists_url":"https://api.github.com/users/nagisa/gists{/gist_id}","starred_url":"https://api.github.com/users/nagisa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nagisa/subscriptions","organizations_url":"https://api.github.com/users/nagisa/orgs","repos_url":"https://api.github.com/users/nagisa/repos","events_url":"https://api.github.com/users/nagisa/events{/privacy}","received_events_url":"https://api.github.com/users/nagisa/received_events","type":"User","site_admin":false},"created_at":"2015-10-13T08:51:48Z","updated_at":"2015-10-13T08:51:48Z","body":"> If you do have a trivial new constructor, should you be able to derive it. I say yes.\r\n\r\nThere’s nothing `derive(Default)` doesn’t solve here.\r\n\r\nIt *is* common, though. Just search through the standard library for `new`. Majority is 1 or 0 argument functions that construct instances structs with private fields.\r\n\r\nI also :-1: here for time being. While I found myself creating private structs with boiler-platey `new` methods pretty often… I don’t find it to be a problem per se, and almost never `#[derive(Default)]` and `fn new() -> Self { Default::default() }` is the same implementation as I want it to be."}}