[英]What does the + operand between types mean?
With this example from core::any 从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);
}
This is the first time I've seen a +
operand between types Any + Debug
. 这是我第一次看到
Any + Debug
类型之间的+
操作数。 I'm assuming its like algebraic types and therefore would be an Any
type with Debug
type; 我假设它像代数类型 ,因此将是带有
Debug
类型的Any
类型; however, I cannot find any docs under algebraic types in Rust. 但是,我在Rust的代数类型下找不到任何文档。
What is the +
actually doing here, what is it called? +
在这里实际执行的操作是什么? Where can I find the docs on this? 在哪里可以找到相关文档?
T: Any + Debug
is a trait bound . T: Any + Debug
是特质绑定 。 The type T
must satisfy Any
and Debug
, therefore the +
sign is used here and it is not related to algebraic types. 类型
T
必须满足Any
和 Debug
,因此此处使用+
号,并且与代数类型无关。 You can read more on traits at the corresponding section in the book . 您可以在本书的相应部分中阅读有关特征的更多信息。
This section mentions the +
sign. 本节提到
+
号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.