簡體   English   中英

類型之間的+操作數是什么意思?

[英]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的代數類型下找不到任何文檔。

+在這里實際執行的操作是什么? 在哪里可以找到相關文檔?

T: Any + Debug特質綁定 類型T必須滿足Any Debug ,因此此處使用+號,並且與代數類型無關。 您可以在本書相應部分中閱讀有關特征的更多信息。

本節提到+號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM