5/5
# Trait ## Example Execute the following command to run [`./solutions/examples/trait.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/trait_basic/solutions/examples/trait.rs) ```shell cargo run --example trait ``` ## Exercises Exercises are in [`./exercises/src/lib.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/trait_basic/exercises/src/lib.rs) ### Exercise 1 Implement trait `Tester` for `Foundry`. Return `forge test `, followed by `file_path`. ### Exercise 2 Implement trait `Tester` for `Cargo`. Return `cargo test `, followed by `file_path`. ### Exercise 3 ```rust pub fn test(tester: ?, file_path: &str) -> String { todo!(); } ``` Fix the code. `tester` is any type that implements the `Tester` trait. Return the output of calling the function `test` on the type the implements the `Tester` trait. ## Test ```shell cargo test ```
Execute the following command to run ./solutions/examples/trait.rs
Exercises are in ./exercises/src/lib.rs
Implement trait Tester
for Foundry
. Return forge test
, followed by file_path
.
Implement trait Tester
for Cargo
. Return cargo test
, followed by file_path
.
Fix the code. tester
is any type that implements the Tester
trait.
Return the output of calling the function test
on the type the implements the Tester
trait.
A hands-on introduction to Rust Traits - Discover how traits define shared behavior, enabling flexible and reusable code in Rust. You'll implement a `Tester` trait for `Foundry` and `Cargo`, then build a generic function that leverages this trait.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025
Rust Developer
Rust Programming BasicsDuration: 6min
Duration: 18min
Duration: 47min
Duration: 15min
Duration: 19min
Duration: 8min
Duration: 12min
Duration: 46min
Duration: 14min
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025