0
0
Rustprogramming~10 mins

Generics with 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 define a generic function that prints any type implementing the Display trait.

Rust
use std::fmt::Display;

fn print_value<T: [1]>(value: T) {
    println!("{}", value);
}

fn main() {
    print_value(42);
    print_value("hello");
}
Drag options to blanks, or click blank then click option'
ADisplay
BCopy
CClone
DDebug
Attempts:
3 left
💡 Hint
Common Mistakes
Using Debug instead of Display causes a formatting error with {}.
Omitting the trait bound causes a compile error.
2fill in blank
medium

Complete the code to implement a trait bound for a generic function that returns the length of any type that implements the Len trait.

Rust
trait Len {
    fn len(&self) -> usize;
}

fn get_length<T: [1]>(item: T) -> usize {
    item.len()
}

struct MyString(String);

impl Len for MyString {
    fn len(&self) -> usize {
        self.0.len()
    }
}

fn main() {
    let s = MyString(String::from("hello"));
    println!("Length: {}", get_length(s));
}
Drag options to blanks, or click blank then click option'
ALen
BClone
CDisplay
DCopy
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated traits like Clone or Display causes errors.
Forgetting the trait bound causes a compile error.
3fill in blank
hard

Fix the error in the generic function by adding the correct trait bound to allow comparison.

Rust
fn largest<T: [1]>(list: &[T]) -> &T {
    let mut largest = &list[0];
    for item in list {
        if item > largest {
            largest = item;
        }
    }
    largest
}

fn main() {
    let numbers = vec![34, 50, 25, 100, 65];
    println!("Largest number is {}", largest(&numbers));
}
Drag options to blanks, or click blank then click option'
ADisplay
BPartialOrd
CCopy
DClone
Attempts:
3 left
💡 Hint
Common Mistakes
Using Copy or Clone does not provide comparison operators.
Omitting the trait bound causes a compile error.
4fill in blank
hard

Fill both blanks to define a generic struct with a trait bound and implement a method that uses the trait.

Rust
use std::fmt::[1];

struct Wrapper<T: [2]> {
    value: T,
}

impl<T: Display> Wrapper<T> {
    fn show(&self) {
        println!("Value: {}", self.value);
    }
}

fn main() {
    let w = Wrapper { value: 123 };
    w.show();
}
Drag options to blanks, or click blank then click option'
ADisplay
BDebug
DClone
Attempts:
3 left
💡 Hint
Common Mistakes
Using Debug instead of Display causes formatting errors.
Mismatching trait bounds between struct and impl causes errors.
5fill in blank
hard

Fill all three blanks to complete a generic function that returns the longer of two string slices using trait bounds.

Rust
fn longest<'a, T: [1] + [2]>(x: &'a T, y: &'a T) -> &'a T {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

fn main() {
    let s1 = String::from("hello");
    let s2 = String::from("world!");
    let result = longest(&s1, &s2);
    println!("Longest string is: {}", result);
}
Drag options to blanks, or click blank then click option'
ADisplay
BAsRef<str>
CPartialOrd
DDebug
Attempts:
3 left
💡 Hint
Common Mistakes
Using only Display without AsRef causes errors calling len().
Omitting trait bounds causes compile errors.