多重限定
使用多重限定(multiple bounds)可以用 + 连接。和平常一样,不同的类型使用 , 隔开。
1. use std::fmt::{Debug, Display};
3. fn compare_prints<T: Debug + Display>(t: &T) {
4. println!("Debug: `{:?}`", t);
5. println!("Display: `{}`", t);
6. }
8. fn compare_types<T: Debug, U: Debug>(t: &T, u: &U) {
9. println!("t: `{:?}", t);
10. println!("u: `{:?}", u);
11. }
13. fn main() {
14. let string = "words";
15. let array = [1, 2, 3];
16. let vec = vec![1, 2, 3];
18. compare_prints(&string);
19. //compare_prints(&array);
20. // 试一试 ^ 将此行注释去掉。
22. compare_types(&array, &vec);
23. }
