Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to implement the trait method for the struct.
Rust
trait Greet {
fn greet(&self) -> String;
}
struct Person {
name: String,
}
impl Greet for Person {
fn greet(&self) -> String {
format!("Hello, {}!", self.[1])
}
}
fn main() {
let p = Person { name: String::from("Alice") };
println!("{}", p.greet());
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' directly instead of 'self.name' inside format!.
Using the struct name 'Person' instead of the field.
✗ Incorrect
The method greet uses self.name to access the name field of the Person struct.
2fill in blank
mediumComplete the code to implement the trait method that returns the area of a shape.
Rust
trait Shape {
fn area(&self) -> f64;
}
struct Circle {
radius: f64,
}
impl Shape for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * self.radius [1] self.radius
}
}
fn main() {
let c = Circle { radius: 3.0 };
println!("Area: {}", c.area());
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - instead of * for multiplication.
Dividing radius by 2 instead of squaring.
✗ Incorrect
The area of a circle is π times the radius squared, so we multiply radius by itself.
3fill in blank
hardFix the error in the trait implementation by completing the missing keyword.
Rust
trait Describe {
fn describe(&self) -> String;
}
struct Book {
title: String,
}
[1] Describe for Book {
fn describe(&self) -> String {
format!("Book: {}", self.title)
}
}
fn main() {
let b = Book { title: String::from("Rust Book") };
println!("{}", b.describe());
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'trait' or 'struct' instead of 'impl'.
Omitting the keyword entirely.
✗ Incorrect
The keyword 'impl' is used to implement a trait for a struct in Rust.
4fill in blank
hardFill both blanks to implement the trait method that returns a formatted string with the struct's fields.
Rust
trait Info {
fn info(&self) -> String;
}
struct Car {
make: String,
year: u32,
}
impl Info for Car {
fn info(&self) -> String {
format!("[1] - [2]", self.make, self.year)
}
}
fn main() {
let car = Car { make: String::from("Toyota"), year: 2020 };
println!("{}", car.info());
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using named placeholders like '{year}' without matching arguments.
Using debug format '{:?}' unnecessarily.
✗ Incorrect
The '{}' placeholder is used to format values in order. Both make and year are formatted with '{}'.
5fill in blank
hardFill all three blanks to implement a trait method that returns the full name from first and last names.
Rust
trait FullName {
fn full_name(&self) -> String;
}
struct User {
first_name: String,
last_name: String,
}
impl FullName for User {
fn full_name(&self) -> String {
format!("[1] [2]", self.[3], self.last_name)
}
}
fn main() {
let user = User { first_name: String::from("John"), last_name: String::from("Doe") };
println!("{}", user.full_name());
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '{:?}' instead of '{}'.
Accessing the wrong field like self.last_name twice.
✗ Incorrect
The format string uses two '{}' placeholders for first and last names. The first_name field is accessed with self.first_name.