diff --git a/src/hello/print.md b/src/hello/print.md index d02ab02e..01eb6bec 100644 --- a/src/hello/print.md +++ b/src/hello/print.md @@ -12,36 +12,55 @@ ```rust,editable,ignore,mdbook-runnable fn main() { - // 通常情况下,`{}` 会被任意变量内容所替换。 - // 变量内容会转化成字符串。 + /* + * 通常情况下,`{}` 会被任意变量内容所替换。 + * 变量内容会转化成字符串。 + * 不加后缀的话,31 就自动成为 i32 类型。 + * 你可以添加后缀来改变 31 的类型(例如使用 31i64 声明 31 为 i64 类型)。 + * 输出结果: 31 days + */ println!("{} days", 31); - // 不加后缀的话,31 就自动成为 i32 类型。 - // 你可以添加后缀来改变 31 的类型(例如使用 31i64 声明 31 为 i64 类型)。 - - // 用变量替换字符串有多种写法。 - // 比如可以使用位置参数。 + /* + * 用变量替换字符串有多种写法。 + * 比如可以使用位置参数。 + * 输出结果: Alice, this is Bob. Bob, this is Alice + */ println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob"); - // 可以使用命名参数。 + /* + * 可以使用命名参数。 + * 输出结果: the quick brown fox jumps over the lazy dog + */ println!("{subject} {verb} {object}", object="the lazy dog", subject="the quick brown fox", verb="jumps over"); - // 可以在 `:` 后面指定特殊的格式。 + /* + * 可以在 `:` 后面指定特殊的格式。 + * b-binary,即二进制形式输出,这里输出了2的二进制形式10 + * 输出结果: 1 of 10 people know binary, the other half don't + */ println!("{} of {:b} people know binary, the other half don't", 1, 2); - // 你可以按指定宽度来右对齐文本。 - // 下面语句输出 " 1",5 个空格后面连着 1。 + /* + * 你可以按指定宽度来右对齐文本。 + * 下面语句输出 " 1",5 个空格后面连着 1。 + */ println!("{number:>width$}", number=1, width=6); // 你可以在数字左边补 0。下面语句输出 "000001"。 println!("{number:>0width$}", number=1, width=6); - // println! 会检查使用到的参数数量是否正确。 + /* + * println! 会检查使用到的参数数量是否正确。 + * 改正 ^ 补上漏掉的参数:"James" + * 输出结果: + * error: invalid reference to positional argument 1 (there is 1 argument) + * 需要修改为:println!("My name is {0}, {1} {0}", "Bond", "James"); + */ println!("My name is {0}, {1} {0}", "Bond"); - // 改正 ^ 补上漏掉的参数:"James" // 创建一个包含单个 `i32` 的结构体(structure)。命名为 `Structure`。 #[allow(dead_code)]