From 9e5bfa601bba960af3b31c36873f6cac210406da Mon Sep 17 00:00:00 2001 From: maoziqiang Date: Thu, 23 Feb 2023 20:37:29 +0800 Subject: [PATCH 1/2] fix: fix lifetime links --- src/basic/compound-type/struct.md | 4 ++-- src/basic/ownership/borrowing.md | 2 +- src/compiler/fight-with-compiler/lifetime/loop.md | 2 +- src/compiler/pitfalls/closure-with-lifetime.md | 2 +- src/index-list.md | 12 ++++++------ src/too-many-lists/ok-stack/iter.md | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/basic/compound-type/struct.md b/src/basic/compound-type/struct.md index bddada23..6d034751 100644 --- a/src/basic/compound-type/struct.md +++ b/src/basic/compound-type/struct.md @@ -223,7 +223,7 @@ impl SomeTrait for AlwaysEqual { 在之前的 `User` 结构体的定义中,有一处细节:我们使用了自身拥有所有权的 `String` 类型而不是基于引用的 `&str` 字符串切片类型。这是一个有意而为之的选择:因为我们想要这个结构体拥有它所有的数据,而不是从其它地方借用数据。 -你也可以让 `User` 结构体从其它对象借用数据,不过这么做,就需要引入[生命周期(lifetimes)](https://course.rs/advance/lifetime/basic.html)这个新概念(也是一个复杂的概念),简而言之,生命周期能确保结构体的作用范围要比它所借用的数据的作用范围要小。 +你也可以让 `User` 结构体从其它对象借用数据,不过这么做,就需要引入[生命周期(lifetimes)](https://course.rs/basic/lifetime.html)这个新概念(也是一个复杂的概念),简而言之,生命周期能确保结构体的作用范围要比它所借用的数据的作用范围要小。 总之,如果你想在结构体中使用一个引用,就必须加上生命周期,否则就会报错: @@ -274,7 +274,7 @@ help: consider introducing a named lifetime parameter | ``` -未来在[生命周期](https://course.rs/advance/lifetime/basic.html)中会讲到如何修复这个问题以便在结构体中存储引用,不过在那之前,我们会避免在结构体中使用引用类型。 +未来在[生命周期](https://course.rs/advance/lifetime/advance.html)中会讲到如何修复这个问题以便在结构体中存储引用,不过在那之前,我们会避免在结构体中使用引用类型。 ## 使用 `#[derive(Debug)]` 来打印结构体的信息 diff --git a/src/basic/ownership/borrowing.md b/src/basic/ownership/borrowing.md index 40a394bb..77de45fa 100644 --- a/src/basic/ownership/borrowing.md +++ b/src/basic/ownership/borrowing.md @@ -270,7 +270,7 @@ help: consider using the `'static` lifetime ``` -错误信息引用了一个我们还未介绍的功能:[生命周期(lifetimes)](https://course.rs/advance/lifetime/basic.html)。不过,即使你不理解生命周期,也可以通过错误信息知道这段代码错误的关键信息: +错误信息引用了一个我们还未介绍的功能:[生命周期(lifetimes)](https://course.rs/basic/lifetime.html)。不过,即使你不理解生命周期,也可以通过错误信息知道这段代码错误的关键信息: ```text this function's return type contains a borrowed value, but there is no value for it to be borrowed from. diff --git a/src/compiler/fight-with-compiler/lifetime/loop.md b/src/compiler/fight-with-compiler/lifetime/loop.md index 3c1355ed..bedfcd8c 100644 --- a/src/compiler/fight-with-compiler/lifetime/loop.md +++ b/src/compiler/fight-with-compiler/lifetime/loop.md @@ -168,7 +168,7 @@ impl A { 我们来逐步深入分析下: -1. 首先为`two`方法增加一下生命周期标识: `fn two<'a>(&'a mut self) -> &'a i32 { .. }`, 这里根据生命周期的[消除规则](https://course.rs/advance/lifetime/basic.html#三条消除规则)添加的 +1. 首先为`two`方法增加一下生命周期标识: `fn two<'a>(&'a mut self) -> &'a i32 { .. }`, 这里根据生命周期的[消除规则](https://course.rs/basic/lifetime.html#三条消除规则)添加的 2. 根据生命周期标识可知:`two`中返回的`k`的生命周期必须是`'a` 3. 根据第 2 条,又可知:`let k = self.one();`中对`self`的借用生命周期也是`'a` 4. 因为`k`的借用发生在`loop`循环内,因此它需要小于等于循环的生命周期,但是根据之前的推断,它又要大于等于函数的生命周期`'a`,而函数的生命周期又大于等于循环生命周期, diff --git a/src/compiler/pitfalls/closure-with-lifetime.md b/src/compiler/pitfalls/closure-with-lifetime.md index eaf70639..9af2d426 100644 --- a/src/compiler/pitfalls/closure-with-lifetime.md +++ b/src/compiler/pitfalls/closure-with-lifetime.md @@ -26,7 +26,7 @@ error: lifetime may not live long enough 咦?竟然报错了,明明两个一模一样功能的函数,一个正常编译,一个却报错,错误原因是编译器无法推测返回的引用和传入的引用谁活得更久! -真的是非常奇怪的错误,学过[Rust 生命周期](https://course.rs/advance/lifetime/basic.html)的读者应该都记得这样一条生命周期消除规则: **如果函数参数中只有一个引用类型,那该引用的生命周期会被自动分配给所有的返回引用**。我们当前的情况完美符合,`fn_elision`函数的顺利编译通过,就充分说明了问题。 +真的是非常奇怪的错误,学过[Rust 生命周期](https://course.rs/basic/lifetime.html#三条消除规则)的读者应该都记得这样一条生命周期消除规则: **如果函数参数中只有一个引用类型,那该引用的生命周期会被自动分配给所有的返回引用**。我们当前的情况完美符合,`fn_elision`函数的顺利编译通过,就充分说明了问题。 那为何闭包就出问题了? diff --git a/src/index-list.md b/src/index-list.md index d9d22926..41407e72 100644 --- a/src/index-list.md +++ b/src/index-list.md @@ -384,13 +384,13 @@ [struct 结构体]: https://course.rs/basic/compound-type/struct.html [self &self &mut self]: https://course.rs/basic/method.html#selfself-和-mut-self [self 与 self]: https://course.rs/basic/trait/trait-object#self-与-self -[生命周期标注语法]: https://course.rs/advance/lifetime/basic.html#生命周期标注语法 -[生命周期消除]: https://course.rs/advance/lifetime/basic.html#生命周期消除 +[生命周期标注语法]: https://course.rs/basic/lifetime.html#生命周期标注语法 +[生命周期消除]: https://course.rs/basic/lifetime.html#生命周期消除 [生命周期消除规则补充]: https://course.rs/advance/lifetime/advance.html#生命周期消除规则补充 -[函数中的生命周期]: https://course.rs/advance/lifetime/basic.html#函数中的生命周期 -[结构体中的生命周期]: https://course.rs/advance/lifetime/basic.html#结构体中的生命周期 -[方法中的生命周期]: https://course.rs/advance/lifetime/basic.html#方法中的生命周期 -[静态生命周期]: https://course.rs/advance/lifetime/basic.html#静态生命周期 +[函数中的生命周期]: https://course.rs/basic/lifetime.html#函数中的生命周期 +[结构体中的生命周期]: https://course.rs/basic/lifetime.html#结构体中的生命周期 +[方法中的生命周期]: https://course.rs/basic/lifetime.html#方法中的生命周期 +[静态生命周期]: https://course.rs/basic/lifetime.html#静态生命周期 [&'static 和 t: 'static]: https://course.rs/advance/lifetime/static.html [back](#head) diff --git a/src/too-many-lists/ok-stack/iter.md b/src/too-many-lists/ok-stack/iter.md index 3944e7cd..632e7df8 100644 --- a/src/too-many-lists/ok-stack/iter.md +++ b/src/too-many-lists/ok-stack/iter.md @@ -416,7 +416,7 @@ test second::test::peek ... ok test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured ``` -最后,还有一点值得注意,之前的代码事实上可以应用[生命周期消除原则](https://course.rs/advance/lifetime/basic.html#生命周期消除): +最后,还有一点值得注意,之前的代码事实上可以应用[生命周期消除原则](https://course.rs/basic/lifetime.html#生命周期消除): ```rust impl List { pub fn iter<'a>(&'a self) -> Iter<'a, T> { From e841b45501a53468311e572c63aa936641855359 Mon Sep 17 00:00:00 2001 From: Sunface Date: Fri, 24 Feb 2023 09:07:05 +0800 Subject: [PATCH 2/2] Update src/basic/compound-type/struct.md --- src/basic/compound-type/struct.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/compound-type/struct.md b/src/basic/compound-type/struct.md index 6d034751..e171b36c 100644 --- a/src/basic/compound-type/struct.md +++ b/src/basic/compound-type/struct.md @@ -274,7 +274,7 @@ help: consider introducing a named lifetime parameter | ``` -未来在[生命周期](https://course.rs/advance/lifetime/advance.html)中会讲到如何修复这个问题以便在结构体中存储引用,不过在那之前,我们会避免在结构体中使用引用类型。 +未来在[生命周期](https://course.rs/basic/lifetime.html)中会讲到如何修复这个问题以便在结构体中存储引用,不过在那之前,我们会避免在结构体中使用引用类型。 ## 使用 `#[derive(Debug)]` 来打印结构体的信息