0
0
Rustprogramming~10 mins

Implementing traits 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 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'
Agreet
Bself
CPerson
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' directly instead of 'self.name' inside format!.
Using the struct name 'Person' instead of the field.
2fill in blank
medium

Complete 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'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - instead of * for multiplication.
Dividing radius by 2 instead of squaring.
3fill in blank
hard

Fix 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'
Aimpl
Buse
Ctrait
Dstruct
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'trait' or 'struct' instead of 'impl'.
Omitting the keyword entirely.
4fill in blank
hard

Fill 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'
A{}
B{:?}
C{year}
D{make}
Attempts:
3 left
💡 Hint
Common Mistakes
Using named placeholders like '{year}' without matching arguments.
Using debug format '{:?}' unnecessarily.
5fill in blank
hard

Fill 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'
A{}
Bfirst_name
Clast_name
D{:?}
Attempts:
3 left
💡 Hint
Common Mistakes
Using '{:?}' instead of '{}'.
Accessing the wrong field like self.last_name twice.