RRust By Example
beginnerModules

Use and Import

Import items with use keyword.

Use and Import

Import items with use keyword.

Difficulty

Beginner

Code

rust
use std::collections::{HashMap, HashSet};
use std::io::{self, Write};

fn main() {
    let mut map = HashMap::new();
    map.insert("key", "value");

    let set: HashSet<i32> = vec![1, 2, 3].into_iter().collect();
    println!("{:?} {:?}", map, set);
}

Explanation

This example demonstrates how to use use and import in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.

Key Concepts

  • Rust's strong type system catches errors at compile time
  • Ownership and borrowing rules ensure memory safety
  • Pattern matching makes code expressive and exhaustive

Related Topics

Browse more examples in the modules category to build a complete understanding of this topic.

More Modules Examples