知识卡片
委托读取绑定数据寿命
Post::content 仅借用 Post。它通过 Option::as_ref 借到当前状态,再由状态决定返回什么。状态的 content<‘a>(&self, post: &‘a Post) -> &‘a str 把切片有效期绑定到实际存放字符串的 Post,区分两个输入引用。未发布状态使用空切片默认实现,已发布状态返回 post.content。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 面向对象编程特性 Requesting a Review Changes the Post’s State · 查看原书依据
Note that we need lifetime annotations on this method, as we discussed in Chapter 10. We’re taking a reference to a post as an argument and returning a reference to part of that post, so the lifetime of the returned reference is related to the lifetime of the post argument.
参考来源
- 位置:《The Rust Programming Language(第2版)》第17章 面向对象编程特性(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c17.xhtml`,块 0157)
- 结论依据:原文说明返回的是文章的一部分,显式生命周期因此关联到 post 参数。
- 原始内容:Note that we need lifetime annotations on this method, as we discussed in Chapter 10. We’re taking a reference to a post as an argument and returning a reference to part of that post, so the lifetime of the returned reference is related to the lifetime of the post argument.