mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-23 07:20:20 +08:00
Update codes
This commit is contained in:
parent
a8739d9eb9
commit
44396e8d4b
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "branches"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,7 @@
|
||||
fn main() {
|
||||
let a = [10, 20, 30, 40, 50];
|
||||
|
||||
for element in a.iter() {
|
||||
for element in a {
|
||||
println!("the value is: {}", element);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "variables"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -7,14 +7,10 @@ error[E0384]: cannot assign twice to immutable variable `x`
|
||||
| -
|
||||
| |
|
||||
| first assignment to `x`
|
||||
| help: make this binding mutable: `mut x`
|
||||
| help: consider making this binding mutable: `mut x`
|
||||
3 | println!("The value of x is: {}", x);
|
||||
4 | x = 6;
|
||||
| ^^^^^ cannot assign twice to immutable variable
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0384`.
|
||||
error: could not compile `variables`
|
||||
|
||||
To learn more, run the command again with --verbose.
|
||||
error: could not compile `variables` due to previous error
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "variables"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "variables"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -2,4 +2,5 @@ $ cargo run
|
||||
Compiling variables v0.1.0 (file:///projects/variables)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
|
||||
Running `target/debug/variables`
|
||||
The value of x is: 12
|
||||
The value of x in the inner scope is: 12
|
||||
The value of x is: 6
|
||||
|
@ -3,7 +3,10 @@ fn main() {
|
||||
|
||||
let x = x + 1;
|
||||
|
||||
let x = x * 2;
|
||||
{
|
||||
let x = x * 2;
|
||||
println!("The value of x in the inner scope is: {}", x);
|
||||
}
|
||||
|
||||
println!("The value of x is: {}", x);
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "variables"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "variables"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -6,9 +6,5 @@ error[E0308]: mismatched types
|
||||
3 | spaces = spaces.len();
|
||||
| ^^^^^^^^^^^^ expected `&str`, found `usize`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
error: could not compile `variables`
|
||||
|
||||
To learn more, run the command again with --verbose.
|
||||
error: could not compile `variables` due to previous error
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "floating-point"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "numeric-operations"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -10,6 +10,7 @@ fn main() {
|
||||
|
||||
// division
|
||||
let quotient = 56.7 / 32.2;
|
||||
let floored = 2 / 3; // Results in 0
|
||||
|
||||
// remainder
|
||||
let remainder = 43 % 5;
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "boolean"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,5 +1,5 @@
|
||||
fn main() {
|
||||
let t = true;
|
||||
|
||||
let f: bool = false; // 使用显式类型标注
|
||||
let f: bool = false; // with explicit type annotation
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "char"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "tuples"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "tuples"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "tuples"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "arrays"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "arrays"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "arrays"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -2,5 +2,4 @@ $ cargo run
|
||||
Compiling functions v0.1.0 (file:///projects/functions)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
|
||||
Running `target/debug/functions`
|
||||
The value of x is: 5
|
||||
The value of y is: 6
|
||||
The measurement is: 5h
|
||||
|
@ -1,8 +1,7 @@
|
||||
fn main() {
|
||||
another_function(5, 6);
|
||||
print_labeled_measurement(5, 'h');
|
||||
}
|
||||
|
||||
fn another_function(x: i32, y: i32) {
|
||||
println!("The value of x is: {}", x);
|
||||
println!("The value of y is: {}", y);
|
||||
fn print_labeled_measurement(value: i32, unit_label: char) {
|
||||
println!("The measurement is: {}{}", value, unit_label);
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -25,9 +25,6 @@ warning: unnecessary parentheses around assigned value
|
||||
|
|
||||
= note: `#[warn(unused_parens)]` on by default
|
||||
|
||||
error: aborting due to 2 previous errors; 1 warning emitted
|
||||
|
||||
For more information about this error, try `rustc --explain E0658`.
|
||||
error: could not compile `functions`
|
||||
|
||||
To learn more, run the command again with --verbose.
|
||||
warning: `functions` (bin "functions") generated 1 warning
|
||||
error: could not compile `functions` due to 2 previous errors; 1 warning emitted
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,6 +1,4 @@
|
||||
fn main() {
|
||||
let x = 5;
|
||||
|
||||
let y = {
|
||||
let x = 3;
|
||||
x + 1
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -10,9 +10,5 @@ error[E0308]: mismatched types
|
||||
8 | x + 1;
|
||||
| - help: consider removing this semicolon
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
error: could not compile `functions`
|
||||
|
||||
To learn more, run the command again with --verbose.
|
||||
error: could not compile `functions` due to previous error
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "comments"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,3 +1,3 @@
|
||||
fn main() {
|
||||
let lucky_number = 7; // 我感觉今天好幸运
|
||||
let lucky_number = 7; // I’m feeling lucky today
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "comments"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,4 +1,4 @@
|
||||
fn main() {
|
||||
// 我感觉今天好幸运
|
||||
// I’m feeling lucky today
|
||||
let lucky_number = 7;
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "branches"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "branches"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "branches"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -6,9 +6,5 @@ error[E0308]: mismatched types
|
||||
4 | if number {
|
||||
| ^^^^^^ expected `bool`, found integer
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
error: could not compile `branches`
|
||||
|
||||
To learn more, run the command again with --verbose.
|
||||
error: could not compile `branches` due to previous error
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "branches"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "branches"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "branches"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -8,9 +8,5 @@ error[E0308]: `if` and `else` have incompatible types
|
||||
| |
|
||||
| expected because of this
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
error: could not compile `branches`
|
||||
|
||||
To learn more, run the command again with --verbose.
|
||||
error: could not compile `branches` due to previous error
|
||||
|
5
listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.lock
generated
Normal file
5
listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/Cargo.lock
generated
Normal file
@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,13 @@
|
||||
$ cargo run
|
||||
Compiling loops v0.1.0 (file:///projects/loops)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.58s
|
||||
Running `target/debug/loops`
|
||||
count = 0
|
||||
remaining = 10
|
||||
remaining = 9
|
||||
count = 1
|
||||
remaining = 10
|
||||
remaining = 9
|
||||
count = 2
|
||||
remaining = 10
|
||||
End count = 2
|
@ -0,0 +1,21 @@
|
||||
fn main() {
|
||||
let mut count = 0;
|
||||
'counting_up: loop {
|
||||
println!("count = {}", count);
|
||||
let mut remaining = 10;
|
||||
|
||||
loop {
|
||||
println!("remaining = {}", remaining);
|
||||
if remaining == 9 {
|
||||
break;
|
||||
}
|
||||
if count == 2 {
|
||||
break 'counting_up;
|
||||
}
|
||||
remaining -= 1;
|
||||
}
|
||||
|
||||
count += 1;
|
||||
}
|
||||
println!("End count = {}", count);
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,7 +1,6 @@
|
||||
[package]
|
||||
name = "no_type_annotations"
|
||||
version = "0.1.0"
|
||||
authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
@ -6,9 +6,5 @@ error[E0282]: type annotations needed
|
||||
2 | let guess = "42".parse().expect("Not a number!");
|
||||
| ^^^^^ consider giving `guess` a type
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0282`.
|
||||
error: could not compile `no_type_annotations`
|
||||
|
||||
To learn more, run the command again with --verbose.
|
||||
error: could not compile `no_type_annotations` due to previous error
|
||||
|
Loading…
Reference in New Issue
Block a user