How to Import Items in Rust with use
Rust code refers to items from other modules and crates by their path, such as std::collections::HashMap. A use declaration brings that path into scope once, so the rest of the file can simply say HashMap.
Rust Programming Language and Its Toolchain
View All TagsRust code refers to items from other modules and crates by their path, such as std::collections::HashMap. A use declaration brings that path into scope once, so the rest of the file can simply say HashMap.
Many Rust crates keep parts of themselves switched off until you ask for them. These optional parts are called features, and turning one on is part of adding the crate: cargo add <crate> --features <feature> writes both into Cargo.toml in one step.
cargo run builds a Rust program when its code has changed and then runs it, all in one command. A few options cover almost everything else: -q to hide Cargo's own messages, -- to pass arguments to your program, and --release for a fast, optimized build.
Cargo, Rust's build tool and package manager, creates new projects with one command. cargo new writes a ready-to-run program, the Cargo.toml that describes it and a Git repository around it, so the first cargo run already prints Hello, world!.