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


Rust LowerExp用法及代码示例


本文简要介绍rust语言中 Trait alloc::fmt::LowerExp 的用法。

用法

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

e 格式化。

LowerExp trait 应使用小写的 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");

在类型上实现LowerExp

use std::fmt;

struct Length(i32);

impl fmt::LowerExp for Length {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let val = f64::from(self.0);
        fmt::LowerExp::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 alloc::fmt::LowerExp。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。