0
0
Rustprogramming~10 mins

Using external crates in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Using external crates
Start Rust Project
Add crate to Cargo.toml
Run cargo build
Import crate in code with use
Call functions/types from crate
Run cargo run to execute
See output using crate features
This flow shows how to add an external crate to a Rust project, import it, and use its features step-by-step.
Execution Sample
Rust
use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    let n: u8 = rng.gen_range(1..=10);
    println!("Random number: {}", n);
}
This code uses the external crate 'rand' to generate and print a random number between 1 and 10.
Execution Table
StepActionCode LineState ChangeOutput
1Import rand crate's Rng traituse rand::Rng;Rng trait available
2Create random number generatorlet mut rng = rand::thread_rng();rng initialized
3Generate random number between 1 and 10let n: u8 = rng.gen_range(1..=10);n assigned random value (e.g. 7)
4Print the random numberprintln!("Random number: {}", n);Random number: 7
5Program ends}
💡 Program ends after printing the random number.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
rnguninitializedrandom number generator instancesamesame
nuninitializeduninitializedrandom u8 between 1 and 10 (e.g. 7)same
Key Moments - 3 Insights
Why do we need to add the crate to Cargo.toml before using it in code?
Adding the crate to Cargo.toml tells Rust's package manager to download and include the crate. Without this, the compiler won't find the crate when you try to use it, as shown in the first step of the execution_table.
What does 'use rand::Rng;' do in the code?
It imports the Rng trait from the rand crate so we can call methods like gen_range on the rng object. Without this import, the method wouldn't be recognized, as seen in step 1 of the execution_table.
How does the program know what range to generate the random number from?
The range 1..=10 is passed to gen_range, which means numbers from 1 to 10 inclusive. This is shown in step 3 where n gets assigned a value within this range.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'n' after step 3?
AUninitialized
BThe number 0
CA random number between 1 and 10
DThe number 11
💡 Hint
Check the 'State Change' column in step 3 of the execution_table.
At which step is the 'rng' variable initialized?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'State Change' columns for when rng gets assigned.
If we forget to add the crate to Cargo.toml, what will happen?
AThe compiler will give an error saying it can't find the crate
BThe program will print a default number 0
CThe program will compile and run normally
DThe program will crash at runtime
💡 Hint
Refer to the key_moments section about adding crates to Cargo.toml.
Concept Snapshot
Using external crates in Rust:
1. Add crate name and version to Cargo.toml
2. Run 'cargo build' to download and compile
3. Import crate items with 'use'
4. Call crate functions/types in your code
5. Run with 'cargo run' to see results
Full Transcript
This visual execution shows how to use an external crate in Rust. First, you add the crate to Cargo.toml so Rust knows to download it. Then you import the crate's features in your code with 'use'. Next, you create objects or call functions from the crate, like generating a random number with the rand crate. Finally, you run the program to see the output. Variables like 'rng' and 'n' change as the program runs, and the output prints the random number. Common confusions include why Cargo.toml is needed, what 'use' does, and how the range for random numbers works.