0
0
Rustprogramming~10 mins

Using external crates in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add the external crate rand in the Cargo.toml file.

Rust
[dependencies]
rand = "[1]"
Drag options to blanks, or click blank then click option'
Arandom
B1.0
C0.8
Dlatest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'latest' or 'random' as version strings which are invalid.
Omitting the version number.
2fill in blank
medium

Complete the code to import the thread_rng function from the rand crate.

Rust
use rand::[1];
Drag options to blanks, or click blank then click option'
ARng
BSeedableRng
Crandom
Dthread_rng
Attempts:
3 left
💡 Hint
Common Mistakes
Importing traits like Rng instead of the function.
Using random which is a different function.
3fill in blank
hard

Fix the error in the code to generate a random number between 1 and 10.

Rust
let mut rng = thread_rng();
let num = rng.gen_range([1]);
Drag options to blanks, or click blank then click option'
A1..10
B1..=10
C0..10
D1, 10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1..10 which excludes 10.
Passing two numbers separated by a comma which is invalid.
4fill in blank
hard

Fill both blanks to import the thread_rng function and the Rng trait from the rand crate.

Rust
use rand::[1];
use rand::[2];
Drag options to blanks, or click blank then click option'
Athread_rng
BRng
Crandom
DSeedableRng
Attempts:
3 left
💡 Hint
Common Mistakes
Importing only the function or only the trait.
Importing unrelated items like random or SeedableRng.
5fill in blank
hard

Fill all three blanks to create a random number generator, generate a number between 5 and 15, and print it.

Rust
use rand::[1];
use rand::[2];

fn main() {
    let mut rng = [3]();
    let num = rng.gen_range(5..=15);
    println!("Random number: {}", num);
}
Drag options to blanks, or click blank then click option'
Athread_rng
BRng
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the function name with the trait name.
Not calling the function to create the generator.