0
0
Rustprogramming~20 mins

Using external crates in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Crate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Rust code using the rand crate?
Consider this Rust program that uses the rand crate to generate a random number between 1 and 3 inclusive. What will it print?
Rust
use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    let n = rng.gen_range(1..=3);
    println!("{}", n);
}
APrints a random number: 0, 1, or 2
BRuntime panic due to invalid range
CCompilation error: gen_range requires exclusive upper bound
DPrints a random number: 1, 2, or 3
Attempts:
2 left
💡 Hint
Check the documentation for gen_range and how inclusive ranges are specified.
Predict Output
intermediate
2:00remaining
What does this code print using the chrono crate?
This Rust program uses the chrono crate to get the current year. What is the output?
Rust
use chrono::Utc;
use chrono::Datelike;

fn main() {
    let year = Utc::now().year();
    println!("{}", year);
}
APrints the current year as a 4-digit number, e.g., 2024
BCompilation error: missing trait import for year()
CPrints the current month number
DRuntime error: Utc::now() returns a string, not a DateTime
Attempts:
2 left
💡 Hint
Look at the chrono crate's DateTime methods.
🔧 Debug
advanced
3:00remaining
Why does this code fail to compile when using the serde_json crate?
This Rust code tries to parse a JSON string into a struct using serde_json but fails. What is the cause?
Rust
use serde::Deserialize;

#[derive(Deserialize)]
struct Person {
    name: String,
    age: u8,
}

fn main() {
    let data = "{\"name\": \"Alice\", \"age\": 30}";
    let p: Person = serde_json::from_str(data).unwrap();
    println!("{} is {} years old", p.name, p.age);
}
APerson struct missing Debug trait for unwrap()
BMissing serde_json crate in Cargo.toml
CSyntax error in JSON string due to unescaped quotes
DAge field type mismatch: u8 cannot hold 30
Attempts:
2 left
💡 Hint
Check the JSON string syntax carefully.
📝 Syntax
advanced
2:00remaining
Which option correctly adds the external crate in Cargo.toml and imports it in Rust?
You want to use the regex crate in your Rust project. Which option correctly shows the Cargo.toml dependency and the import statement?
A
[dependencies]
regex = 1

use regex::Regex;
B
[dependencies]
regex = "1"

use regex::Regex;
C
[dependencies]
regex = "^1.0"

use regex::Regex;
D
[dependencies]
regex = "=1.0"

use regex::regex;
Attempts:
2 left
💡 Hint
Check the correct syntax for specifying versions in Cargo.toml and the correct import path.
🚀 Application
expert
3:00remaining
How many items does this code produce using itertools crate?
This Rust code uses the itertools crate to create a Cartesian product of two vectors. How many pairs are printed?
Rust
use itertools::iproduct;

fn main() {
    let a = vec![1, 2, 3];
    let b = vec![4, 5];
    let pairs: Vec<_> = iproduct!(a.iter(), b.iter()).collect();
    println!("{}", pairs.len());
}
A6
B5
C3
D2
Attempts:
2 left
💡 Hint
Cartesian product pairs every element of the first vector with every element of the second.