Disabling standard namespaces

Rust seems to look like usein some namespaces. For example, I do not need to use std::string::Stringand just type String. How can I define my own struct Stringwithout colliding with std::string::String? And where can I find a list of namespaces that are enabled by default?

+4
source share
2 answers

You can create your own Stringusing ... create your own line:

struct String {
    len: u8,
}

fn main() {}

You can then eliminate which Stringyou want to use fully defined paths:

fn main() {
    // String::new();
    // error: type `String` does not implement any method in scope named `new`

    std::string::String::new();
}

A complete list of automatically imported items can be found in the prelude (version 1, at the time of writing).

+5

" ", .

+6

All Articles