当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。