预定义宏

  • print!
  • println!
  • format!
  • format_arg!
  • write!

打印

Debug与Display

格式化字符串

位置参数

let s = format!("{0} + {1} = {2}",10, 20, 30);

键值参数

let s = format!("{one:?} + {two:?} = {three:?}",one=10, two=20, three=30);

例:正确示例。

fn main() {
    let s = format!("今天是{0}年{1}月{2}日, {week:?}, 气温{3:>0width$} ~ {4:>0width$} 摄氏度。",
        2016, 11, 24, 3, -6, week = "Thursday", width = 2);

    print!("{}", s);
}

format!宏调用的时候参数可以使任意类型。而且可以把位置参数键值参数混用。但要注意,位置参数必须在键值参数之前,否则无法通过编译。

例:位置参数必须在键值参数之前,以下案例编译失败。

fn main() {rust
    let s = format!("今天是{0}年{1}月{2}日, {week:?}, 气温{3:>0width$} ~ {4:>0width$} 摄氏度。",
        2016, 11, 24, week = "Thursday", 3, -6, width = 2);

    print!("{}", s);
}

results matching ""

    No results matching ""