← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.f.9.e.3 · 生成模型:未记录

结构体可拥有或借用字段

理解等级:中级

存 String 使结构体管理文本资源,可独立持有。存 &str 时,实例仍只借用别处的文本,类型声明需表达字段引用的生命周期,让编译器防止结构体被用到引用失效以后。

相关卡片 [[引用不能活过数据]]、[[借用以引用传递访问]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第05章 用结构体组织相关数据(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c05.xhtml`,块 0053) - 结论依据:原文把 User 选择 String 和字段由实例拥有的理由直接连接起来。 - 原始内容:In the User struct definition in Listing 5-1, we used the owned String type rather than the &str string slice type. This is a deliberate choice because we want each instance of this struct to own all of its data and for that data to be valid for as long as the entire struct is valid.