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
}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::fmt::Display;pub fn compare_and_display<T: Display + PartialOrd>(val_1: T, val_2: T) -> T { println!("{val_1} {val_2}"); if val_1 > val_2 {val_1} else {val_2}} // 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>(val_1: T, val_2: T) -> T { println!("{val_1} {val_2}"); if val_1 > val_2 {val_1} else {val_2}} // 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: 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.use std::fmt::Display;use std::cmp::PartialOrd;pub fn compare_and_display<T>(value1: T, value2: T) -> T where T: Display + PartialOrd, { if value1 > value2 { return value1; } else { return 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::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) -> Twhere T: Display + PartialOrd{ println!("{}", param1); println!("{}", param2); if param1 > param2 { return param1 } else { return 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.use std::fmt::Display;use std::cmp::PartialOrd;pub fn compare_and_display<T>(value1: T, value2: T) -> T where T: Display + PartialOrd, { println!("{}", value1); println!("{}", value2); if value1 > value2 { return value1; } else { 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;pub fn compare_and_display<T: Display + PartialOrd> (a: T, b: T) -> T { println!("{} {}", a, b); if a.gt(&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>(item1: T, item2: T) -> Twhere T: PartialOrd + Display,{ println!("{item1} {item2}"); if item1 > item2 { item1 } else { item2 } // match item1.gt(&item2) { // true => item1, // false => 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);}
// 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 { 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>(a: T, b: T) -> Twhere T: std::fmt::Display + PartialOrd { 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);}
// TODO: Define the generic function `compare_and_display` with appropriate trait bounds.use std::{fmt::Display,cmp::PartialOrd};pub fn compare_and_display<T>(v1 : T,v2 : T) -> Twhere T : Display + PartialOrd,{ println!("{v1}, {v2}"); return 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.pub fn compare_and_display<T>(param1: T, param2: T) -> Twhere T: std::fmt::Display + PartialOrd{ println!("Param 1: {}, Param 2: {}", param1, param2); if param1 > param2 { return param1; } 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;pub fn compare_and_display<T : Display + PartialOrd> (a : T, b : T) -> T { println!("{a} {b}"); if a.gt(&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: Display + PartialOrd>(value1: T, value2: T) -> T { println!("{value1} {value2}"); if value1 > value2 { value1 } 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;pub fn compare_and_display<T : Display + PartialOrd> (a : T, b : T) -> T { 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.pub fn compare_and_display<T: std::fmt::Display + PartialOrd>(x: T, y: T) -> T { println!("{x} {y}"); 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.pub fn compare_and_display <T: PartialOrd + std::fmt::Display> (val1: T, val2: T) -> T { print!("{} {}",val1, val2); 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);}
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: PartialOrd + Display>(value1: T, value2: T) -> T { println!("First value {}", value1); println!("Second value {}", value2); 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);}
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);}