Struct alloc::fmt::Arguments1.0.0[−][src]

pub struct Arguments<'a> { /* fields omitted */ }
Expand description

该结构体表示格式字符串及其参数的安全预编译版本。 由于无法安全地完成此操作,因此无法在运行时生成该文件,因此未提供任何构造函数,并且该字段为私有字段以防止修改。

format_args! 宏将安全地创建此结构体的实例。 宏在编译时验证格式字符串,因此可以安全地执行 write() 和 format() 函数的使用。

您可以在 Debug 和 Display 上下文中使用 format_args! 返回的 Arguments<'a>,如下所示。 该示例还显示 Debug 和 Display 的格式相同: format_args! 中的插值格式字符串。

let debug = format!("{:?}", format_args!("{} foo {:?}", 1, 2));
let display = format!("{}", format_args!("{} foo {:?}", 1, 2));
assert_eq!("1 foo 2", display);
assert_eq!(display, debug);
Run

Implementations

获取格式化的字符串 (如果没有要格式化的参数)。

在最琐碎的情况下,这可以用来避免分配。

Examples

use std::fmt::Arguments;

fn write_str(_: &str) { /* ... */ }

fn write_fmt(args: &Arguments) {
    if let Some(s) = args.as_str() {
        write_str(s)
    } else {
        write_str(&args.to_string());
    }
}
Run
assert_eq!(format_args!("hello").as_str(), Some("hello"));
assert_eq!(format_args!("").as_str(), Some(""));
assert_eq!(format_args!("{}", 1).as_str(), None);
Run

Trait Implementations

返回值的副本。 Read more

从 source 执行复制分配。 Read more

使用给定的格式化程序格式化该值。 Read more

使用给定的格式化程序格式化该值。 Read more

Auto Trait Implementations

Blanket Implementations

获取 self 的 TypeId。 Read more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

获得所有权后的结果类型。

通常通过克隆从借用数据中创建拥有的数据。 Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

recently added

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more

将给定值转换为 String。 Read more

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。