← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 10650.b · 生成模型:未记录

委托读取绑定数据寿命

理解等级:中级

Post::content 仅借用 Post。它通过 Option::as_ref 借到当前状态,再由状态决定返回什么。状态的 content<‘a>(&self, post: &‘a Post) -> &‘a str 把切片有效期绑定到实际存放字符串的 Post,区分两个输入引用。未发布状态使用空切片默认实现,已发布状态返回 post.content。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《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.