Skip to main content

new

Derive Macro new 

Source
#[derive(new)]
{
    // Attributes available to this derive:
    #[new]
}
Expand description

See crate-level docs for a usage and output example.

§Container options

OptDefaultDescription
new(const_fn)falseWhether to make the constructor a const fn
defaultfalseGenerate a Default implementation if the constructor ends up with no arguments.
new(vis(visibility))pubThe visibility of the constructor
new(name(ident))newConstructor fn name
new(comment(literal))A doc comment to add to the constructor
new(bounds(T: Whatever))Generic type bounds for the implementation
new(args(foo: u8, bar: String))Additional arguments to add to the beginning of the args list

§Field options

OptDescription
new(default)Omit the field from the constructor and use Default
new(clone)Make the argument pass-by-reference and clone it
new(into)Make the argument Into<T>
new(name(ident))Rename the function argument - useful for newtype structs
new(val(expr))Initialise the value with the following expression instead of a constructor argument