From e6eab1377fac3f36f915f22f7e0efd92b7e245b5 Mon Sep 17 00:00:00 2001 From: Bassem Girgis Date: Sat, 17 Jul 2021 13:48:36 -0500 Subject: [PATCH] 1st --- Cargo.lock | 7 +++++++ src/main.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c8b6e39 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "by-example-01-02-formatted-print" +version = "0.1.0" diff --git a/src/main.rs b/src/main.rs index e7a11a9..73fa1c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,47 @@ fn main() { - println!("Hello, world!"); + // 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:>0width$}", 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. }