new: Constructor functions

Rust does not have a new keyword, instead new is a common prefix or whole method name.

#![allow(unused)]
fn main() {
impl <T> Vec<T> {
    // Creates an empty vec.
    fn new() -> Vec<T>;
}

impl <T> Box<T> {
    fn new(T) -> Box<T>;
}
}
  • There’s no new keyword for rust to initialize a new value, only functions you call or values you directly populate.

    new is conventional for the “default” constructor function for a type. It holds no special syntactic meaning.

    This is sometimes a prefix, it sometimes takes arguments.