mirror of https://github.com/KaiserY/trpl-zh-cn
parent
fef1dba8b0
commit
fbdc47c405
@ -0,0 +1,19 @@
|
||||
$ cargo build
|
||||
Compiling restaurant v0.1.0 (file:///projects/restaurant)
|
||||
error[E0433]: failed to resolve: use of undeclared crate or module `hosting`
|
||||
--> src/lib.rs:11:9
|
||||
|
|
||||
11 | hosting::add_to_waitlist();
|
||||
| ^^^^^^^ use of undeclared crate or module `hosting`
|
||||
|
||||
warning: unused import: `crate::front_of_house::hosting`
|
||||
--> src/lib.rs:7:5
|
||||
|
|
||||
7 | use crate::front_of_house::hosting;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `#[warn(unused_imports)]` on by default
|
||||
|
||||
For more information about this error, try `rustc --explain E0433`.
|
||||
warning: `restaurant` (lib) generated 1 warning
|
||||
error: could not compile `restaurant` due to previous error; 1 warning emitted
|
@ -1,9 +1,14 @@
|
||||
fn main() {
|
||||
// ANCHOR: here
|
||||
{
|
||||
let v = vec![1, 2, 3, 4];
|
||||
let v = vec![1, 2, 3, 4, 5];
|
||||
|
||||
// 处理变量 v
|
||||
} // <- 这里 v 离开作用域并被丢弃
|
||||
let third: &i32 = &v[2];
|
||||
println!("The third element is {third}");
|
||||
|
||||
let third: Option<&i32> = v.get(2);
|
||||
match third {
|
||||
Some(third) => println!("The third element is {third}"),
|
||||
None => println!("There is no third element."),
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
@ -0,0 +1,16 @@
|
||||
$ cargo run
|
||||
Compiling collections v0.1.0 (file:///projects/collections)
|
||||
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
|
||||
--> src/main.rs:6:5
|
||||
|
|
||||
4 | let first = &v[0];
|
||||
| - immutable borrow occurs here
|
||||
5 |
|
||||
6 | v.push(6);
|
||||
| ^^^^^^^^^ mutable borrow occurs here
|
||||
7 |
|
||||
8 | println!("The first element is: {first}");
|
||||
| ----- immutable borrow later used here
|
||||
|
||||
For more information about this error, try `rustc --explain E0502`.
|
||||
error: could not compile `collections` due to previous error
|
@ -1,8 +1,11 @@
|
||||
fn main() {
|
||||
// ANCHOR: here
|
||||
let v = vec![1, 2, 3, 4, 5];
|
||||
let mut v = vec![1, 2, 3, 4, 5];
|
||||
|
||||
let does_not_exist = &v[100];
|
||||
let does_not_exist = v.get(100);
|
||||
let first = &v[0];
|
||||
|
||||
v.push(6);
|
||||
|
||||
println!("The first element is: {first}");
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
fn main() {
|
||||
// ANCHOR: here
|
||||
let mut v = vec![1, 2, 3, 4, 5];
|
||||
|
||||
let first = &v[0];
|
||||
|
||||
v.push(6);
|
||||
|
||||
println!("The first element is: {}", first);
|
||||
let v = vec![100, 32, 57];
|
||||
for i in &v {
|
||||
println!("{i}");
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
fn main() {
|
||||
// ANCHOR: here
|
||||
let v = vec![100, 32, 57];
|
||||
for i in &v {
|
||||
println!("{}", i);
|
||||
let mut v = vec![100, 32, 57];
|
||||
for i in &mut v {
|
||||
*i += 50;
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
@ -1,8 +1,15 @@
|
||||
fn main() {
|
||||
// ANCHOR: here
|
||||
let mut v = vec![100, 32, 57];
|
||||
for i in &mut v {
|
||||
*i += 50;
|
||||
enum SpreadsheetCell {
|
||||
Int(i32),
|
||||
Float(f64),
|
||||
Text(String),
|
||||
}
|
||||
|
||||
let row = vec![
|
||||
SpreadsheetCell::Int(3),
|
||||
SpreadsheetCell::Text(String::from("blue")),
|
||||
SpreadsheetCell::Float(10.12),
|
||||
];
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
@ -1,15 +1,9 @@
|
||||
fn main() {
|
||||
// ANCHOR: here
|
||||
enum SpreadsheetCell {
|
||||
Int(i32),
|
||||
Float(f64),
|
||||
Text(String),
|
||||
}
|
||||
{
|
||||
let v = vec![1, 2, 3, 4];
|
||||
|
||||
let row = vec![
|
||||
SpreadsheetCell::Int(3),
|
||||
SpreadsheetCell::Text(String::from("blue")),
|
||||
SpreadsheetCell::Float(10.12),
|
||||
];
|
||||
// do stuff with v
|
||||
} // <- v goes out of scope and is freed here
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::fs::File;
|
||||
|
||||
fn main() {
|
||||
let f = File::open("hello.txt");
|
||||
let greeting_file_result = File::open("hello.txt");
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::fs::File;
|
||||
|
||||
fn main() {
|
||||
let f = File::open("hello.txt")?;
|
||||
let greeting_file = File::open("hello.txt")?;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::fs::File;
|
||||
|
||||
fn main() {
|
||||
let f = File::open("hello.txt").unwrap();
|
||||
let greeting_file = File::open("hello.txt").unwrap();
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
use std::fs::File;
|
||||
|
||||
fn main() {
|
||||
let f = File::open("hello.txt").expect("Failed to open hello.txt");
|
||||
let greeting_file = File::open("hello.txt")
|
||||
.expect("hello.txt should be included in this project");
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
struct Pair<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
}
|
||||
|
||||
impl<T> Pair<T> {
|
||||
fn new(x: T, y: T) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Display + PartialOrd> Pair<T> {
|
||||
fn cmp_display(&self) {
|
||||
if self.x >= self.y {
|
||||
println!("The largest member is x = {}", self.x);
|
||||
} else {
|
||||
println!("The largest member is y = {}", self.y);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
$ cargo run
|
||||
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
|
||||
error[E0597]: `x` does not live long enough
|
||||
--> src/main.rs:6:13
|
||||
|
|
||||
6 | r = &x;
|
||||
| ^^ borrowed value does not live long enough
|
||||
7 | }
|
||||
| - `x` dropped here while still borrowed
|
||||
8 |
|
||||
9 | println!("r: {}", r);
|
||||
| - borrow later used here
|
||||
|
||||
For more information about this error, try `rustc --explain E0597`.
|
||||
error: could not compile `chapter10` due to previous error
|
@ -0,0 +1,10 @@
|
||||
fn main() {
|
||||
let r;
|
||||
|
||||
{
|
||||
let x = 5;
|
||||
r = &x;
|
||||
}
|
||||
|
||||
println!("r: {}", r);
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
We have some weird comments pointing out borrowing scopes that we don't want to change;
|
||||
unfortunately I haven't found a way to skip them with rustfmt that works so for now we're going to
|
||||
manually skip those listings. See: https://github.com/rust-lang/rustfmt/issues/4028
|
@ -1,14 +1,10 @@
|
||||
fn main() {
|
||||
// ANCHOR: here
|
||||
{
|
||||
let r;
|
||||
|
||||
{
|
||||
let x = 5;
|
||||
r = &x;
|
||||
}
|
||||
|
||||
println!("r: {}", r);
|
||||
}
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
let r; // ---------+-- 'a
|
||||
// |
|
||||
{ // |
|
||||
let x = 5; // -+-- 'b |
|
||||
r = &x; // | |
|
||||
} // -+ |
|
||||
// |
|
||||
println!("r: {}", r); // |
|
||||
} // ---------+
|
||||
|
@ -1,14 +1,8 @@
|
||||
fn main() {
|
||||
// ANCHOR: here
|
||||
{
|
||||
let r; // ---------+-- 'a
|
||||
let x = 5; // ----------+-- 'b
|
||||
// |
|
||||
{ // |
|
||||
let x = 5; // -+-- 'b |
|
||||
r = &x; // | |
|
||||
} // -+ |
|
||||
// |
|
||||
println!("r: {}", r); // |
|
||||
} // ---------+
|
||||
// ANCHOR_END: here
|
||||
}
|
||||
let r = &x; // --+-- 'a |
|
||||
// | |
|
||||
println!("r: {}", r); // | |
|
||||
// --+ |
|
||||
} // ----------+
|
||||
|
@ -1,12 +1,7 @@
|
||||
fn main() {
|
||||
// ANCHOR: here
|
||||
{
|
||||
let x = 5; // ----------+-- 'b
|
||||
// |
|
||||
let r = &x; // --+-- 'a |
|
||||
// | |
|
||||
println!("r: {}", r); // | |
|
||||
// --+ |
|
||||
} // ----------+
|
||||
// ANCHOR_END: here
|
||||
let string1 = String::from("abcd");
|
||||
let string2 = "xyz";
|
||||
|
||||
let result = longest(string1.as_str(), string2);
|
||||
println!("The longest string is {}", result);
|
||||
}
|
||||
|
@ -0,0 +1,16 @@
|
||||
$ cargo run
|
||||
Compiling chapter10 v0.1.0 (file:///projects/chapter10)
|
||||
error[E0106]: missing lifetime specifier
|
||||
--> src/main.rs:9:33
|
||||
|
|
||||
9 | fn longest(x: &str, y: &str) -> &str {
|
||||
| ---- ---- ^ expected named lifetime parameter
|
||||
|
|
||||
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`
|
||||
help: consider introducing a named lifetime parameter
|
||||
|
|
||||
9 | fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||
| ++++ ++ ++ ++
|
||||
|
||||
For more information about this error, try `rustc --explain E0106`.
|
||||
error: could not compile `chapter10` due to previous error
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue