5/5
# Trait bound ## Example Execute the following command to run [`./solutions/examples/trait_bound.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/trait_bound/solutions/examples/trait_bound.rs) ```shell cargo run --example trait_bound ``` ## Exercises Exercises are in [`./exercises/src/lib.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/trait_bound/exercises/src/lib.rs) ### Exercise 1 ```rust pub fn min(x: u32, y: u32) -> u32 { if x <= y { x } else { y } } ``` Update the function so that the inputs are generic type that implements the `PartialOrd` trait. ### Exercise 2 ```rust pub fn zip(a: Vec<u32>, b: Vec<i32>) -> Vec<(u32, i32)> { let mut v = vec![]; let len = min(a.len(), b.len()); for i in 0..len { v.push((a[i], b[i])); } v } ``` Update the function so that the inputs are generic types, not necessary the same type, both implements the `Copy` trait. ## Test ```shell cargo test ```
Execute the following command to run ./solutions/examples/trait_bound.rs
Exercises are in ./exercises/src/lib.rs
Update the function so that the inputs are generic type that implements the PartialOrd
trait.
Update the function so that the inputs are generic types, not necessary the same type, both implements the Copy
trait.
A foundational guide to Rust's Trait Bound - Discover how trait bounds enable you to write generic functions that work with any type implementing specific behaviors. You'll put this knowledge to use by generalizing a `min` function with the `PartialOrd` trait and a `zip` function with the `Copy` 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