use and Paths
Import items.
use and Paths is a free Learn Rust Coding lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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"));
}Absolute vs Relative Paths
An absolute path starts from crate (the crate root). A relative path starts from the current module, optionally using self or super.
mod a {
pub mod b {
pub fn hi() { println!("hi from b"); }
}
pub fn call_b() {
crate::a::b::hi();
self::b::hi();
}
}
fn main() {
a::call_b();
}use Inside Modules
A use only affects the scope it appears in. Place it at the top of a module to shorten names used throughout that module.
mod work {
use std::collections::HashSet;
pub fn run() {
let mut s = HashSet::new();
s.insert(1);
s.insert(1);
println!("size {}", s.len());
}
}
fn main() {
work::run();
}Renaming With as
When two items share a name, or a name is too long, rename on import with as.
use std::collections::HashMap as Map;
fn main() {
let mut m: Map<&str, i32> = Map::new();
m.insert("x", 1);
println!("{:?}", m.get("x"));
}Grouping Imports
Import several items from the same path in one line using braces. This keeps the import block tidy.
use std::collections::{HashMap, HashSet};
fn main() {
let map: HashMap<i32, i32> = HashMap::new();
let set: HashSet<i32> = HashSet::new();
println!("{} {}", map.len(), set.len());
}Nested Groups and self
Inside a group, self imports the module itself alongside its items. This lets you use both the module name and specific members.
use std::io::{self, Write};
fn main() {
let _ = io::stdout().write_all(b"hello\n");
}Glob Imports
The glob * brings in everything public from a path. Use it sparingly — it can hide where names come from — but it is common for preludes and tests.
mod colors {
pub fn red() -> &'static str { "red" }
pub fn blue() -> &'static str { "blue" }
}
use colors::*;
fn main() {
println!("{} {}", red(), blue());
}Idiomatic Import Style
Convention: import functions by their parent module (use std::cmp; cmp::max(...)) but import types by name (use std::collections::HashMap;). This makes calls read clearly.
use std::cmp;
fn main() {
println!("{}", cmp::max(3, 9));
}Paths to External Crates
For dependencies, the path starts with the crate name, for example rand::thread_rng. You list the crate in Cargo.toml, then import items the same way you do for std.
Keeping Imports Clean
Group related imports, rename to avoid clashes, and prefer explicit imports over globs in application code. Clean imports make a file easy to scan.
Quick Check
Test your path knowledge.
Recap
You learned to import items:
usebrings an item into scope by a short name- Absolute paths start at
crate; relative paths useself/super asrenames, braces group, and*globs- Import types by name and functions by parent module for clarity
Frequently asked questions
Is the “use and Paths” lesson free?
Yes — the full text of “use and Paths” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “use and Paths”?
Import items. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “use and Paths” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.