Now that we know a bit about traits and generics, let's dive into trait bounds. Trait bounds allow you to specify that a generic type must implement a particular trait. This ensures that only types with certain capabilities can be used with your generic code.
For example:
std::fmt::Display
trait for printing.PartialOrd
for comparison.Trait bounds make your code flexible while maintaining strong type safety.
You can specify trait bounds in two ways:
Inline: Specify the trait bounds after the impl
or fn
keyword.
fn my_function<T: Trait1 + Trait2>(value: T) {
// code here
}
Where clause: Use a where
clause to specify trait bounds.
fn my_function<T>(value: T)
where
T: Trait1 + Trait2,
{
// code here
}
This means that the generic type T
must implement both Trait1
and Trait2
.
Define a generic function compare_and_display
that:
Display
trait.PartialOrd
trait.Use trait bounds to ensure the function works only with types that implement both Display
and PartialOrd
.
If you're stuck, here are some hints to help you solve the challenge:
std::cmp::PartialOrd
to compare values.std::fmt::Display
to print values with the println!
macro.T: Trait1 + Trait2
or using a where
clause. e.g.
fn my_function<T: Trait1 + Trait2>(value: T) {
// code here
}
use std::fmt::{Display};use std::cmp::{max, PartialOrd};// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(value1: T, value2: T) -> T { println!("{value1} {value2}"); if value1 > value2 { value1 }else{ value2 }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;pub fn compare_and_display<T>(item1: T, item2: T) -> Twhere T: Display + PartialOrd,{ if item1 > item2 { item1 } else { item2 }}
use std::cmp::PartialOrd;use std::fmt::Display;pub fn compare_and_display<T: Display + PartialOrd>( t1: T, t2: T) -> T{ println!("{t1} {t2}"); if t1 > t2 {return t1} if t1 < t2 {return t2} return t1}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(x: T, y: T) -> T where T: Display + PartialOrd { if let true = x > y { return x; } y} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(first: T, second: T) -> Twhere T: std::fmt::Display + PartialOrd { if first > second { first } else { second }} // Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.//use std::cmp::Ordering;use std::cmp::PartialOrd;use std::fmt::Display;pub fn compare_and_display<T> (l: T, r: T) -> T whereT: Display + PartialOrd { println!("{l} {r}"); match l.ge(&r) { true => l, false => r, }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::cmp::PartialOrd;use std::fmt::Display;pub fn compare_and_display<T: PartialOrd + Display>(a: T, b: T) -> T { if a > b {a} else {b}} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// use std::cmp::{ Ordering, Ord };use std::fmt;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: PartialOrd + fmt::Display>(lv: T, rv: T) -> T { if lv.gt(&rv) { lv } else { rv }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(a: T, b:T) -> T { if a > b { a } else { b }} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(left: T, right: T) -> T { if left > right { left } else { right }} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(left: T, right: T) -> T where T: Display + PartialOrd { if left > right { return left } right} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(value1: T, value2: T) -> T// Complete the function definitionwhere T: Display + PartialOrd,{ println!("{value1}, {value2}"); if value1 > value2 { value1 } else { value2 }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(lhs: T, rhs: T) -> T where T: std::fmt::Display + PartialOrd { if lhs.gt(&rhs) { lhs } else { rhs }} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(lhs: T, rhs: T) -> T where T: std::fmt::Display + PartialOrd { if lhs < rhs { rhs } else { lhs }} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::{fmt::Display};pub fn compare_and_display<T: PartialOrd + Display>(p1: T, p2: T) -> T { println!("{p1},{p2}"); if p1.gt(&p2) { p1 } else { p2 }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(a: T, b: T) -> Twhere T: std::fmt::Display + PartialOrd,{ println!("{}", a); println!("{}", b); if a > b { return a; }; return b;}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::{cmp::Ordering, fmt::Display};// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<'a, T>(lhs:T, rhs: T) -> T where T: Display + PartialOrd { match lhs.partial_cmp(&rhs) { Some(Ordering::Less) => rhs, Some(Ordering::Greater) => lhs, Some(Ordering::Equal) => lhs, None => todo!(), } } // Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
pub fn compare_and_display<T: std::cmp::PartialOrd>(value1: T, value2: T) -> T { if value1 > value2 { value1 } else { value2 }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::fmt::Display;pub fn compare_and_display<T>(p1: T, p2: T) -> Twhere T: Display + PartialOrd,{ print!("{p1} {p2}"); if p1.gt(&p2) { return p1; } p2} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(a: T, b: T) -> T { if a > b { return a; } b}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
pub fn compare_and_display<T>(value1: T, value2: T) -> Twhere T: std::fmt::Display + std::cmp::PartialOrd,{ if value1 >= value2 { value1 } else { value2 }}pub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(a: T, b: T) -> T { if a > b { a } else { b }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: PartialOrd + Display>(a:T, b:T) -> T // Complete the function definition{ println!("{}{}",a,b); if a < b { return b; } else { return a; }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(lft: T, rht: T) -> Twhere T: core::fmt::Display + PartialOrd,{ if lft > rht { lft } else { rht }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(first: T, second: T) -> T { println!("first: {}, second: {}", first, second); if first > second { return first; } second}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::cmp::PartialOrd;use std::fmt::Display;use std::cmp::Ordering;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(obj1:T, obj2:T) -> Twhere T: Display + PartialOrd{ println!("{} {}",obj1, obj2); if obj1 > obj2 { obj1 } else { obj2 }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(item:T, item2: T) -> Twhere T: std::fmt::Display + std::cmp::PartialOrd { // Complete the function definition println!{"item: {}, item2: {}", item, item2 } if item > item2 { item } else { item2 }} // Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display; // TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(value1: T, value2: T) -> T{ println!("{}",value1); println!("{}",value2); if value1 > value2 { return value1; } value2}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(a: T, b: T) -> Twhere T: std::fmt::Display + PartialOrd{ println!("{a}"); println!("{b}"); return if a > b { a } else { b };}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::cmp::PartialOrd;use std::fmt::Display;pub fn compare_and_display<T: PartialOrd + Display>(a: T, b: T) -> T { println!("{}", a); println!("{}", b); if a < b { b } else { a }} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: PartialOrd>(a: T, b: T) -> T { if a > b { a } else { b }} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display< T: std::fmt::Display + PartialOrd> (a: T, b: T) -> T { if a > b { a } else { b }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;use std::cmp::PartialOrd;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(param1: T, param2: T) -> T { // Complete the function definition println!("{param1},{param2}"); if param1 < param2 { param2 } else { param1 }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;use std::cmp::PartialOrd;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(param1: T, param2: T) -> T { // Complete the function definition println!("{}",format!("param1: {}, param2: {}",param1, param2)); if param1 < param2 { param2 } else { param1 }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(a: T, b: T) -> Twhere T: std::cmp::PartialOrd + std::fmt::Display,{ println!("{a}, {b}"); if a > b { a } else { b }} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;pub fn compare_and_display<T>(a: T, b: T) -> Twhere T: PartialOrd + Display,{ println!("{a},{b}"); if a > b { a } else { b }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;pub fn compare_and_display<T>(a: T, b: T) -> Twhere T: PartialOrd + Display,{ if a > b { a } else { b }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use core::fmt::Display;use std::cmp::PartialOrd;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(x:T, y:T ) -> Twhere T: Display + PartialOrd,{ println!("{x}{y}"); if x > y { x }else{ y }} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::cmp::PartialOrd;use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(a: T, b: T) -> T { println!("{} {}", a, b); if a >= b { return a; } else { return b; }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::fmt::Display;pub fn compare_and_display<T: Display + PartialOrd>(a: T, b: T) -> T { println!("{a} {b}"); if a < b { b } else { a }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;pub fn compare_and_display<T>(a: T, b: T) -> T where T: Display + PartialOrd{ println!("{a}"); println!("{b}"); if a >= b { a } else { b }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::fmt::Display;pub fn compare_and_display<T: Display + PartialOrd>(a: T, b: T) -> T { println!("{a} {b}"); if a < b { b } else { a }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;pub fn compare_and_display<T>(a: T, b: T) -> Twhere T: Display + PartialOrd{ println!("{}, {}", a.to_string(), b.to_string()); if a > b {a} else {b}}pub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(param1: T, param2: T) -> T{ println!("Param1: {param1}"); println!("Param2: {param2}"); match param1.gt(¶m2) { true => param1, false => param2 }} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(a: T, b: T) -> T // Complete the function definitionwhere T: Display + PartialOrd,{ println!("First argument is: {a}; second argument is {b}"); if a > b {a} else {b}}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::cmp::PartialOrd;use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(v1: T, v2: T) -> T { if v1 > v2 {return v1}; return v2;} // Complete the function definition// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(t1: T, t2: T) -> T { // Complete the function definition if t1 >= t2 { return t1; } else { return t2; } } // Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::cmp::Ordering;use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: PartialOrd + Display>(lhs: T, rhs: T) -> T { println!("Comparing {} and {}", lhs, rhs); if lhs > rhs { lhs } else { rhs }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(t :T, u: T) -> T{ if t > u{ return t; } else { return u; }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::fmt::Display;pub fn compare_and_display<T: Display + PartialOrd>(value: T, value2: T) -> T { // code here if value > value2 { value } else { value2 }}// Example usagepub fn main() { let greater = compare_and_display(10, 20); println!("Greater value: {}", greater); let greater = compare_and_display("Apple", "Orange"); println!("Greater value: {}", greater);}