Update listing code in ch03

Signed-off-by: Sefank <12670778+Sefank@users.noreply.github.com>
This commit is contained in:
Sefank 2022-07-04 10:48:13 +08:00
parent 860f99a134
commit 7090f728ec
No known key found for this signature in database
GPG Key ID: 8257999DC508E901
23 changed files with 33 additions and 37 deletions

View File

@ -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}");
}

View File

@ -2,7 +2,7 @@ fn main() {
let mut number = 3;
while number != 0 {
println!("{}!", number);
println!("{number}!");
number -= 1;
}

View File

@ -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}");
}
}

View File

@ -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

View File

@ -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}");
}

View File

@ -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}");
}

View File

@ -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}");
}

View File

@ -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;
}

View File

@ -1,5 +1,5 @@
fn main() {
let t = true;
let f: bool = false; // 显式指定类型注解
let f: bool = false; // with explicit type annotation
}

View File

@ -1,5 +1,5 @@
fn main() {
let c = 'z';
let z = '';
let z: char = ''; // with explicit type annotation
let heart_eyed_cat = '😻';
}

View File

@ -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}");
}

View File

@ -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}");
}

View File

@ -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}");
}

View File

@ -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}");
}

View File

@ -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

View File

@ -4,5 +4,5 @@ fn main() {
x + 1
};
println!("The value of y is: {}", y);
println!("The value of y is: {y}");
}

View File

@ -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}");
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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}");
}

View File

@ -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}");
}

View File

@ -9,5 +9,5 @@ fn main() {
}
};
println!("The result is {}", result);
println!("The result is {result}");
}

View File

@ -1,6 +1,6 @@
fn main() {
for number in (1..4).rev() {
println!("{}!", number);
println!("{number}!");
}
println!("LIFTOFF!!!");
}