Help us understand the problem. What is going on with this article?

Rustのprintln!マクロ

More than 3 years have passed since last update.

出力機能はstd::fmtで定義されるマクロによって扱われます。

  • format!: フォーマットされたテキストを文字列(String)型に書き込む
  • print!: format!に加え、コンソールにそのテキストを出力
  • println!: print!に加え、改行をつけてコンソールに出力

基本

println!("{} days", 31);

引数の位置を指定

println!("{1} is {0}.", "Bob", "He");

引数の名前で指定

println!("{a} {b} {c}",
             a=123,
             b=456,
             c=789);

フォーマット型の指定

:のあとにフォーマットの型を指定(bは二進数)

println!("{:b}", 2);

幅を指定して右寄せで出力

println!("{number:>width$}", number=1, width=6);
  • 0埋めも可能
  • 出力は"000001"
println!("{number:>0width$}", number=1, width=6);

中括弧{}をそのまま出力

  • {{}}と書く
println!("This is {}, {{}}", "Brace");
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした