From af9a9b6d4f8ffbf2cb47a386c3de4ef4669b950b Mon Sep 17 00:00:00 2001 From: sunface Date: Tue, 25 Jan 2022 10:53:55 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=E8=BF=87=E5=A4=A702?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- book/contents/fight-with-compiler/lifetime/too-long2.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/book/contents/fight-with-compiler/lifetime/too-long2.md b/book/contents/fight-with-compiler/lifetime/too-long2.md index 4df7203d..2b97aa6f 100644 --- a/book/contents/fight-with-compiler/lifetime/too-long2.md +++ b/book/contents/fight-with-compiler/lifetime/too-long2.md @@ -120,7 +120,11 @@ impl<'a> Writer<'a> { ... } ``` -好像有点问题,`indent`返回的`Writer`的生命周期和外面的`Writer`的生命周期一模一样,这很不合理,一眼就能看出前者远小于后者,那我们尝试着修改下`indent`: +好像有点问题,`indent`返回的`Writer`的生命周期和外面调用者的`Writer`的生命周期一模一样,这很不合理,一眼就能看出前者远小于后者。 + +这里稍微展开以下,为何`indent`方法返回值的生命周期不能与参数中的`self`相同。**首先,我们假设它们可以相同,也就是上面的代码可以编译通过**,由于此时在返回值中借用了`self`的可变引用,意味着**如果你在返回值被使用后,还继续使用`self` 会导致重复借用的错误,因为返回值的生命周期将持续到 `self` 结束**。 + +既然不能相同,那我们尝试着修改下`indent`: ```rust fn indent<'b>(&'b mut self) -> Writer<'b> { Writer {