Mat*_*ood 1 static-typing algebraic-data-types rust
从core :: any得到的这个例子
use std::fmt::Debug;
use std::any::Any;
// Logger function for any type that implements Debug.
fn log<T: Any + Debug>(value: &T) {
let value_any = value as &dyn Any;
// try to convert our value to a String. If successful, we want to
// output the String's length as well as its value. If not, it's a
// different type: just print it out unadorned.
match value_any.downcast_ref::<String>() {
Some(as_string) => {
println!("String ({}): {}", as_string.len(), as_string);
}
None => {
println!("{:?}", value);
}
}
}
// This function wants to log its parameter out prior to doing work with it.
fn do_work<T: Any + Debug>(value: &T) {
log(value);
// ...do some other work
}
fn main() {
let my_string = "Hello World".to_string();
do_work(&my_string);
let my_i8: i8 = 100;
do_work(&my_i8);
}
Run Code Online (Sandbox Code Playgroud)
这是我第一次看到+ 类型之间的操作数Any + Debug。我假设它像代数类型,因此将是具有Any类型的Debug类型。但是,我在Rust的代数类型下找不到任何文档。
什么是+真正在这里做,什么是它叫什么名字?在哪里可以找到相关文档?