add code
This commit is contained in:
7
14.Generics/14.5 Multiple bounds/Cargo.lock
generated
Normal file
7
14.Generics/14.5 Multiple bounds/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "multiple_bounds"
|
||||
version = "0.1.0"
|
||||
8
14.Generics/14.5 Multiple bounds/Cargo.toml
Normal file
8
14.Generics/14.5 Multiple bounds/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "multiple_bounds"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
30
14.Generics/14.5 Multiple bounds/src/main.rs
Normal file
30
14.Generics/14.5 Multiple bounds/src/main.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
//!
|
||||
//! 多重泛型约束
|
||||
//! 多重约束可以在每个约束之间使用 `+` 号连接,
|
||||
//! 多个泛型之间使用 `,` 号间隔
|
||||
//!
|
||||
//! 同样支持在 `<T: bound + bound2>` 或者 where `T: bound + bound2` 中使用
|
||||
//!
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
fn compare_prints<T: Debug + Display>(t: &T) {
|
||||
println!("Debug: `{:?}`", t);
|
||||
println!("Display: `{}`", t);
|
||||
}
|
||||
|
||||
fn compare_types<T: Debug, U: Debug>(t: &T, u: &U) {
|
||||
println!("t: `{:?}`", t);
|
||||
println!("u: `{:?}`", u);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let string = "words";
|
||||
let array = [1, 2, 3];
|
||||
let vec = vec![1, 2, 3];
|
||||
|
||||
compare_prints(&string);
|
||||
// compare_prints(&array);
|
||||
// TODO ^ 移除注释查看错误
|
||||
|
||||
compare_types(&array, &vec);
|
||||
}
|
||||
Reference in New Issue
Block a user