mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2024-11-09 08:51:18 +08:00
Update listing code in ch03
Signed-off-by: Sefank <12670778+Sefank@users.noreply.github.com>
This commit is contained in:
parent
860f99a134
commit
7090f728ec
@ -2,5 +2,5 @@ fn main() {
|
||||
let condition = true;
|
||||
let number = if condition { 5 } else { 6 };
|
||||
|
||||
println!("The value of number is: {}", number);
|
||||
println!("The value of number is: {number}");
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ fn main() {
|
||||
let mut number = 3;
|
||||
|
||||
while number != 0 {
|
||||
println!("{}!", number);
|
||||
println!("{number}!");
|
||||
|
||||
number -= 1;
|
||||
}
|
||||
|
@ -2,6 +2,6 @@ fn main() {
|
||||
let a = [10, 20, 30, 40, 50];
|
||||
|
||||
for element in a {
|
||||
println!("the value is: {}", element);
|
||||
println!("the value is: {element}");
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ error[E0384]: cannot assign twice to immutable variable `x`
|
||||
| |
|
||||
| first assignment to `x`
|
||||
| help: consider making this binding mutable: `mut x`
|
||||
3 | println!("The value of x is: {}", x);
|
||||
3 | println!("The value of x is: {x}");
|
||||
4 | x = 6;
|
||||
| ^^^^^ cannot assign twice to immutable variable
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
let x = 5;
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of x is: {x}");
|
||||
x = 6;
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of x is: {x}");
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
let mut x = 5;
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of x is: {x}");
|
||||
x = 6;
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of x is: {x}");
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ fn main() {
|
||||
|
||||
{
|
||||
let x = x * 2;
|
||||
println!("The value of x in the inner scope is: {}", x);
|
||||
println!("The value of x in the inner scope is: {x}");
|
||||
}
|
||||
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of x is: {x}");
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
fn main() {
|
||||
// 加法
|
||||
// addition
|
||||
let sum = 5 + 10;
|
||||
|
||||
// 减法
|
||||
// subtraction
|
||||
let difference = 95.5 - 4.3;
|
||||
|
||||
// 乘法
|
||||
// multiplication
|
||||
let product = 4 * 30;
|
||||
|
||||
// 除法
|
||||
// division
|
||||
let quotient = 56.7 / 32.2;
|
||||
let floored = 2 / 3; // 结果为 0
|
||||
let floored = 2 / 3; // Results in 0
|
||||
|
||||
// 取余
|
||||
// remainder
|
||||
let remainder = 43 % 5;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
fn main() {
|
||||
let t = true;
|
||||
|
||||
let f: bool = false; // 显式指定类型注解
|
||||
let f: bool = false; // with explicit type annotation
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
fn main() {
|
||||
let c = 'z';
|
||||
let z = 'ℤ';
|
||||
let z: char = 'ℤ'; // with explicit type annotation
|
||||
let heart_eyed_cat = '😻';
|
||||
}
|
||||
|
@ -3,5 +3,5 @@ fn main() {
|
||||
|
||||
let (x, y, z) = tup;
|
||||
|
||||
println!("The value of y is: {}", y);
|
||||
println!("The value of y is: {y}");
|
||||
}
|
||||
|
@ -18,8 +18,5 @@ fn main() {
|
||||
|
||||
let element = a[index];
|
||||
|
||||
println!(
|
||||
"The value of the element at index {} is: {}",
|
||||
index, element
|
||||
);
|
||||
println!("The value of the element at index {index} is: {element}");
|
||||
}
|
||||
|
@ -3,5 +3,5 @@ fn main() {
|
||||
}
|
||||
|
||||
fn another_function(x: i32) {
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of x is: {x}");
|
||||
}
|
||||
|
@ -3,5 +3,5 @@ fn main() {
|
||||
}
|
||||
|
||||
fn print_labeled_measurement(value: i32, unit_label: char) {
|
||||
println!("The measurement is: {}{}", value, unit_label);
|
||||
println!("The measurement is: {value}{unit_label}");
|
||||
}
|
||||
|
@ -8,14 +8,13 @@ error: expected expression, found statement (`let`)
|
||||
|
|
||||
= note: variable declaration using `let` is a statement
|
||||
|
||||
error[E0658]: `let` expressions in this position are experimental
|
||||
error[E0658]: `let` expressions in this position are unstable
|
||||
--> src/main.rs:2:14
|
||||
|
|
||||
2 | let x = (let y = 6);
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
|
||||
= help: you can write `matches!(<expr>, <pattern>)` instead of `let <pattern> = <expr>`
|
||||
|
||||
warning: unnecessary parentheses around assigned value
|
||||
--> src/main.rs:2:13
|
||||
|
@ -4,5 +4,5 @@ fn main() {
|
||||
x + 1
|
||||
};
|
||||
|
||||
println!("The value of y is: {}", y);
|
||||
println!("The value of y is: {y}");
|
||||
}
|
||||
|
@ -5,5 +5,5 @@ fn five() -> i32 {
|
||||
fn main() {
|
||||
let x = five();
|
||||
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of x is: {x}");
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
fn main() {
|
||||
let x = plus_one(5);
|
||||
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of x is: {x}");
|
||||
}
|
||||
|
||||
fn plus_one(x: i32) -> i32 {
|
||||
|
@ -1,7 +1,7 @@
|
||||
fn main() {
|
||||
let x = plus_one(5);
|
||||
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of x is: {x}");
|
||||
}
|
||||
|
||||
fn plus_one(x: i32) -> i32 {
|
||||
|
@ -3,5 +3,5 @@ fn main() {
|
||||
|
||||
let number = if condition { 5 } else { "six" };
|
||||
|
||||
println!("The value of number is: {}", number);
|
||||
println!("The value of number is: {number}");
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
fn main() {
|
||||
let mut count = 0;
|
||||
'counting_up: loop {
|
||||
println!("count = {}", count);
|
||||
println!("count = {count}");
|
||||
let mut remaining = 10;
|
||||
|
||||
loop {
|
||||
println!("remaining = {}", remaining);
|
||||
println!("remaining = {remaining}");
|
||||
if remaining == 9 {
|
||||
break;
|
||||
}
|
||||
@ -17,5 +17,5 @@ fn main() {
|
||||
|
||||
count += 1;
|
||||
}
|
||||
println!("End count = {}", count);
|
||||
println!("End count = {count}");
|
||||
}
|
||||
|
@ -9,5 +9,5 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
println!("The result is {}", result);
|
||||
println!("The result is {result}");
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
for number in (1..4).rev() {
|
||||
println!("{}!", number);
|
||||
println!("{number}!");
|
||||
}
|
||||
println!("LIFTOFF!!!");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user