[英]What does “mismatched types: expected `()`” mean when using an if expression?
[英]What does the + operand between types mean?
从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);
}
这是我第一次看到Any + Debug
类型之间的+
操作数。 我假设它像代数类型 ,因此将是带有Debug
类型的Any
类型; 但是,我在Rust的代数类型下找不到任何文档。
+
在这里实际执行的操作是什么? 在哪里可以找到相关文档?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.