Complete the code to define a function that accepts any type implementing the Display trait.
fn print_value<T: [1]>(value: T) { println!("{}", value); }
The Display trait allows formatting with {} in println!. The function requires T to implement Display.
Complete the code to add a trait bound requiring T to implement both Display and Clone.
fn clone_and_print<T: [1] + Clone>(value: T) { let _copy = value.clone(); println!("{}", value); }
The function requires T to implement Display for printing and Clone for cloning. The bound T: Display + Clone expresses this.
Fix the error in the function signature by adding the correct trait bound for T to be printable with {:?}.
fn debug_print<T: [1]>(value: T) { println!("{:?}", value); }
The Debug trait allows formatting with {:?}. To use println!("{:?}", value), T must implement Debug.
Fill both blanks to define a function that accepts any type T implementing both Debug and PartialEq traits.
fn compare_and_print<T: [1] + [2]>(a: T, b: T) { if a == b { println!("Equal: {:?}", a); } else { println!("Not equal: {:?} and {:?}", a, b); } }
The function compares two values with ==, so T must implement PartialEq. It also prints with {:?}, so Debug is needed. The bound is T: Debug + PartialEq.
Fill both blanks to create a function that returns the larger of two values implementing PartialOrd and Debug.
fn max_value<T: [1] + [2]>(a: T, b: T) -> T { if a > b { println!("Max is {:?}", a); a } else { println!("Max is {:?}", b); b } }
The function compares two values with >, so PartialOrd is required. It prints with {:?}, so Debug is also required. The bound is T: PartialOrd + Debug.