Reputation: 21926
If you have a struct like so:
pub struct Foo {
pub a: i32,
pub b: i32,
pub c: String,
// and a bunch of other fields
}
Is there any way to declare a constructor that takes the members of the struct without copy/pasting all umpteen field names/types:
impl Foo {
pub fn new(/* maaaagic */) {
}
}
or do I have to do
impl Foo {
pub fn new(
a: i32,
b: i32,
c: String,
// and a bunch of other args
) {
}
}
Upvotes: 1
Views: 525
Reputation: 60051
If you are using rust-analyzer, there is a generate new assist that does what you want.
Will generate:
impl Foo {
pub fn new(a: i32, b: i32, c: String) -> Self { Self { a, b, c } }
}
Upvotes: 6