Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'latest' or 'random' as version strings which are invalid.
Omitting the version number.
✗ Incorrect
The
rand crate version 0.8 is a valid and commonly used version. You specify the version number as a string in Cargo.toml.2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing traits like
Rng instead of the function.Using
random which is a different function.✗ Incorrect
The
thread_rng function provides a random number generator local to the current thread.3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
1..10 which excludes 10.Passing two numbers separated by a comma which is invalid.
✗ Incorrect
The range
1..=10 includes both 1 and 10, generating numbers from 1 to 10 inclusive.4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing only the function or only the trait.
Importing unrelated items like
random or SeedableRng.✗ Incorrect
You import
thread_rng function to get the random number generator and Rng trait to use methods like gen_range.5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the function name with the trait name.
Not calling the function to create the generator.
✗ Incorrect
You import
thread_rng function and Rng trait, then call thread_rng() to get the generator.