當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Rust UpperExp用法及代碼示例

本文簡要介紹rust語言中 Trait std::fmt::UpperExp 的用法。

用法

pub trait UpperExp {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>;
}

E 格式化。

UpperExp 特征應使用大寫的 E 以科學計數法格式化其輸出。

有關格式化程序的更多信息,請參閱the module-level documentation

例子

f64 的基本用法:

let x = 42.0; // 42.0 is '4.2E1' in scientific notation

assert_eq!(format!("{:E}", x), "4.2E1");

在類型上實現UpperExp

use std::fmt;

struct Length(i32);

impl fmt::UpperExp for Length {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let val = f64::from(self.0);
        fmt::UpperExp::fmt(&val, f) // delegate to f64's implementation
    }
}

let l = Length(100);

assert_eq!(
    format!("l in scientific notation is: {:E}", l),
    "l in scientific notation is: 1E2"
);

assert_eq!(
    format!("l in scientific notation is: {:05E}", l),
    "l in scientific notation is: 001E2"
);

相關用法


注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Trait std::fmt::UpperExp。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。