0Pricing
Learn Rust Coding · Lesson

use and Paths

Import items.

Bringing Items Into Scope

Typing full paths like std::collections::HashMap everywhere is tedious. The use keyword brings an item into scope so you can refer to it by a short name.

A Basic use

After a use, you reference the item by its final name. Here we import HashMap and use it directly.

use std::collections::HashMap;

fn main() {
    let mut scores = HashMap::new();
    scores.insert("ana", 10);
    println!("{:?}", scores.get("ana"));
}

All lessons in this course

  1. Modules and mod
  2. pub and Visibility
  3. use and Paths
  4. Crates and the Crate Root
← Back to Learn Rust Coding