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;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: Display + PartialOrd>(x: T, y: T) -> T { if x > y { x } else { y }}// 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: Display + PartialOrd>(x:T, y:T)-> T{ format!("{x}, {y}"); if x > y { return x }else { return y }}// 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>(left: T, right: T) -> Twhere T: Display + PartialOrd,{ println!("{left}"); println!("{right}"); if left > right { return left; } { return right; }}// 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;// 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); println!("{}", second); if first > second { return first } second} // 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;use std::cmp::PartialOrd;// 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}"); 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);}
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) -> Twhere 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);}
use std::{cmp::Ordering, 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); println!("{}", b); match a.partial_cmp(&b) { Some(Ordering::Equal) => a, Some(Ordering::Greater) => a, Some(Ordering::Less) => b, _ => panic!("Unexpected ordering"), }}// 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<T: Display + PartialOrd>(a: T, b: T) -> T { println!("{}", a); println!("{}", b); match a.partial_cmp(&b) { Some(Ordering::Equal) => a, Some(Ordering::Greater) => a, Some(Ordering::Less) => b, _ => panic!("Unexpected ordering"), }}// 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.// Complete the function definitionpub fn compare_and_display<T: std::fmt::Display + std::cmp::PartialOrd>(v1: T, v2: T) -> T { if v1 > v2 { v1 } else { v2 }}// 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>(v1: T, v2: T) -> Twhere T: Display + PartialOrd,{ if v1 > v2 { v1 } else { 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);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::fmt::Display;use std::cmp::PartialOrd;pub fn compare_and_display<T>(a: T, b: T) -> Twhere T: Display + PartialOrd{ 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>(arg1: T, arg2: T) -> Twhere T: Display + PartialOrd { println!("The arguments are {arg1} and {arg2}"); if arg1 > arg2 { return arg1; } arg2}// 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>(v1: T, v2: T) -> Twhere T: Display + PartialOrd,{ if v1 > v2 { return v1; } v2}// 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::cmp::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;use std::cmp::PartialOrd;// 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,{ 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>(a: T, b: T) -> T // Complete the function definitionwhere T: Display + PartialOrd,{ 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>(a: T, b: T) -> Twhere T: Display + PartialOrd{ 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 { 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.pub fn compare_and_display<T>(a: T, b: T) -> Twhere T: std::fmt::Display + PartialOrd,{ println!("{a} {b}"); if b > a { 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,{ 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.pub fn compare_and_display<T: std::fmt::Display + std::cmp::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);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::cmp::Ordering::*;pub fn compare_and_display<T>(left: T, right: T) -> Twhere T: PartialOrd + std::fmt::Display { println!("left: {}, right: {}", left, right); match left.partial_cmp(&right) { Some(Greater) => left, Some(Less) => right, Some(Equal) => left, None => left, } } // 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;use std::cmp::Ordering;// 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 { println!("{a} {b}");match a.partial_cmp(&b) { Some(Ordering::Greater) => a, _ => 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;use std::cmp::PartialOrd;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(a: T, b: T) -> Twhere T: PartialOrd + Display{ println!("{} {}", a, 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>(value: T, value2: T) -> Twhere T: std::cmp::PartialOrd + std::fmt::Display { if (value > value2) { value } else { value2 }} // 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;use std::cmp::PartialOrd;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(value: T, value2: T) -> Twhere T: Display + PartialOrd { if value > value2 { return value; } return 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;use std::cmp::PartialOrd;// 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, { if value1 > value2 { return value1; } return 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> (value1: T, value2: T) -> Twhere T: std::fmt::Display + std::cmp::PartialOrd,{ 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::cmp::PartialOrd;use std::fmt::Display;pub fn compare_and_display<T>(T1: T, T2: T) -> Twhere T: Display + PartialOrd,{ if T1 > T2{ T1 } else{ 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);}
// 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 + std::cmp::PartialOrd { if lhs > 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.use std::cmp::PartialOrd;use std::fmt::Display;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::cmp::PartialOrd;use std::fmt::Display;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(param1: T, param2: T) -> T where T: Display + PartialOrd{ if param1 > param2 { param1 } else { 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);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T: PartialOrd>(a: T, b: T) -> T { match a.partial_cmp(&b) { Some(std::cmp::Ordering::Greater) => { // println!("{} is greater than {}", a, b); a } Some(std::cmp::Ordering::Less) => { // println!("{} is less than {}", a, b); b } Some(std::cmp::Ordering::Equal) => { // println!("{} is equal to {}", a, b); a // or b, since they are equal } None => { // Handle the case where comparison is not possible panic!( "Cannot compare values of type {:?}", std::any::type_name::<T>() ); } }} // 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:Display+PartialOrd>(lhs:T, rhs:T) -> T { println!("{lhs} {rhs}"); return if lhs < rhs {rhs} else {lhs};}// 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>(lhs:T, rhs:T) -> T { println!("{}", lhs); println!("{}", rhs); return if lhs < rhs {rhs} else {lhs};}// 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) -> Twhere T: Display + PartialOrd{ 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);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds. // Complete the function definitionpub fn compare_and_display<T>(value_1: T, value_2 : T) -> Twhere T:std::cmp::PartialOrd + std::fmt::Display + std::fmt::Debug{ let greater = if value_1 > value_2 { value_1 } else { value_2 }; println!("Greater value: {:?}", greater); return greater;}// 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;pub fn compare_and_display<T: Display + PartialOrd>(a: T, b: T) -> T { println!("First value: {}, second value: {}", 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);}
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{ if first > second { return first; } second} // 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;use std::cmp::PartialOrd;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(aa: T, bb: T) -> Twhere T: Display + PartialOrd,{ println!("{aa} {bb}"); if aa >= bb { aa } else { bb }}// 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) -> T where T:std::fmt::Display + std::cmp::PartialOrd,{ println!("Parameter 1: {}, Parameter 2: {}", 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);}
use std::fmt::Display;pub fn compare_and_display<T>(x: T, y: T) -> Twhere T: Display + PartialOrd,{ if x > y { return x; } else { return y; }}// 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, 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!("Comparing: {} and {}", 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::cmp::max;// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.pub fn compare_and_display<T>(value: T, value2: T) -> Twhere T: std::fmt::Display + PartialOrd,{ println!("{}\n{}", value, value2); if value >= value2 { value } else { value2 }} // 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.use std::cmp::PartialOrd;use std::fmt::Display;pub fn compare_and_display<T: PartialOrd + Display>(first: T, second: T) -> T { println!("{} {}", 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::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) ->T where T:Display + PartialOrd,{ if x >= y { x } else { y }}// 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: PartialOrd>(value1: T, value2: T) -> T { if value1 < value2 { return value2 } value1}// 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;use std::cmp::PartialOrd;pub fn compare_and_display<T>(val1 :T, val2: T) -> T where T: Display+PartialOrd { // Complete the function definition if val1 > val2 { val1 }else { val2 }}// 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.// Complete the function definitionpub fn compare_and_display<T>(a: T, b: T) -> Twhere T: std::fmt::Display + PartialOrd,{ 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.pub fn compare_and_display<T>(a:T,b:T)->T// Complete the function definitionwhere T:std::fmt::Display+PartialOrd{ 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);}