Formatted print

4612 ワード

https://doc.rust-lang.org/rust-by-example/hello/print.html
std::fmtで定義されたマクロ処理の一連の印刷
  • format! : フォーマットされたテキストを文字列
  • に書き込む
  • print! : フォーマットテキストをコンソールに書き込む(io::stdout)
  • println! : フォーマットされたテキストをコンソール(io::stdout)に書き込み、最後に全行文字(n)
  • を追加します.
  • eprint! : フォーマットテキストをコンソールに書き込む(io::stderr)
  • eprintln! : コンソール(io::stderr)にフォーマットテキストを書き込み、最後に1行の文字(n)
  • を追加します.
    fn main() {
        // In general, the `{}` will be automatically replaced with any
        // arguments. These will be stringified.
        println!("{} days", 31);
    
        // Without a suffix, 31 becomes an i32. You can change what type 31 is
        // by providing a suffix. The number 31i64 for example has the type i64.
    
        // There are various optional patterns this works with. Positional
        // arguments can be used.
        println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
    
        // As can named arguments.
        println!("{subject} {verb} {object}",
                 object="the lazy dog",
                 subject="the quick brown fox",
                 verb="jumps over");
    
        // Special formatting can be specified after a `:`.
        println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
    
        // You can right-align text with a specified width. This will output
        // "     1". 5 white spaces and a "1".
        println!("{number:>width$}", number=1, width=6);
    
        // You can pad numbers with extra zeroes. This will output "000001".
        println!("{number:0>width$}", number=1, width=6);
    
        // Rust even checks to make sure the correct number of arguments are
        // used.
        println!("My name is {0}, {1} {0}", "Bond");
        // FIXME ^ Add the missing argument: "James"
    
        // Create a structure named `Structure` which contains an `i32`.
        #[allow(dead_code)]
        struct Structure(i32);
    
        // However, custom types such as this structure require more complicated
        // handling. This will not work.
        println!("This struct `{}` won't print...", Structure(3));
        // FIXME ^ Comment out this line.
    }

    std::fmtには、多くの制御テキスト表示の特性が含まれています.
  • fmt::Debug : {:?}フォーマット
  • を使用してデバッグ
  • fmg::表示:{},和に
  • を表示

    Activity

  • FIXMEセクションを参照して、エラー
  • を修正します.
  • std:::fmtドキュメントに
  • のprintln構文を追加し、pi=3.141592をスクリーン上の3.142に丸めます.
    fn main() {
        // In general, the `{}` will be automatically replaced with any
        // arguments. These will be stringified.
        println!("{} days", 31);
    
        // Without a suffix, 31 becomes an i32. You can change what type 31 is
        // by providing a suffix. The number 31i64 for example has the type i64.
    
        // There are various optional patterns this works with. Positional
        // arguments can be used.
        println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
    
        // As can named arguments.
        println!("{subject} {verb} {object}",
                 object="the lazy dog",
                 subject="the quick brown fox",
                 verb="jumps over");
    
        // Special formatting can be specified after a `:`.
        println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
    
        // You can right-align text with a specified width. This will output
        // "     1". 5 white spaces and a "1".
        println!("{number:>width$}", number=1, width=6);
    
        // You can pad numbers with extra zeroes. This will output "000001".
        println!("{number:0>width$}", number=1, width=6);
    
        // Rust even checks to make sure the correct number of arguments are
        // used.
        // println!("My name is {0}, {1} {0}", "Bond");
        // FIXME ^ Add the missing argument: "James"
        println!("My name is {0}, {1} {0}", "Bond", "James");
    
        // Create a structure named `Structure` which contains an `i32`.
        #[allow(dead_code)]
        struct Structure(i32);
    
        // However, custom types such as this structure require more complicated
        // handling. This will not work.
        //println!("This struct `{}` won't print...", Structure(3));
        // FIXME ^ Comment out this line.
    }