From 035fdb91bc68acb65874f205af23e204b6446b29 Mon Sep 17 00:00:00 2001 From: Nak0_x Date: Wed, 2 Sep 2026 23:52:35 +0200 Subject: [PATCH] tp: Formatting print --- formatted-print/Cargo.lock | 7 +++++ formatted-print/src/main.rs | 56 ++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 formatted-print/Cargo.lock diff --git a/formatted-print/Cargo.lock b/formatted-print/Cargo.lock new file mode 100644 index 0000000..1973110 --- /dev/null +++ b/formatted-print/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "formatted-print" +version = "0.1.0" diff --git a/formatted-print/src/main.rs b/formatted-print/src/main.rs index e7a11a9..eb222cb 100644 --- a/formatted-print/src/main.rs +++ b/formatted-print/src/main.rs @@ -1,3 +1,57 @@ fn main() { - println!("Hello, world!"); + // In general, the `{}` will be automatically replaced with any + // arguments. These will be stringified. + println!("{} days", 31); + + // Positional arguments can be used. Specifying an integer inside `{}` + // determines which additional argument will be replaced. Arguments start + // at 0 immediately after the format string. + 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"); + + // Different formatting can be invoked by specifying the format character + // after a `:`. + println!("Base 10: {}", 69420); // 69420 + println!("Base 2 (binary): {:b}", 69420); // 10000111100101100 + println!("Base 8 (octal): {:o}", 69420); // 207454 + println!("Base 16 (hexadecimal): {:x}", 69420); // 10f2c + + // You can right-justify text with a specified width. This will + // output " 1". (Four white spaces and a "1", for a total width of 5.) + println!("{number:>5}", number=1); + + // You can pad numbers with extra zeroes, + println!("{number:->5}", number=1); // 00001 + // and left-adjust by flipping the sign. This will output "10000". + println!("{number:-<5}", number=1); // 10000 + + // You can use named arguments in the format specifier by appending a `$`. + println!("{number:->width$}", number=1, width=5); + + // Rust even checks to make sure the correct number of arguments are used. + println!("My name is {0}, {1} {0}", "Bond", "James"); + // FIXME ^ Add the missing argument: "James" + + // Only types that implement fmt::Display can be formatted with `{}`. User- + // defined types do not implement fmt::Display by default. + + #[allow(dead_code)] // disable `dead_code` which warn against unused module + struct Structure(i32); + + // This will not compile because `Structure` does not implement + // fmt::Display. + // println!("This struct `{}` won't print...", Structure(3)); + // TODO ^ Try uncommenting this line + + // For Rust 1.58 and above, you can directly capture the argument from a + // surrounding variable. Just like the above, this will output + // " 1", 4 white spaces and a "1". + let number: f64 = 1.0; + let width: usize = 5; + println!("{number:>width$}"); }