← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.g.8 · 生成模型:未记录

动态尺寸类型放在指针后

理解等级:中级

str 的字节长度按值而变,不能直接当普通定长局部变量。&str 携带地址和长度,指针自身尺寸固定,Box 也可持有动态内容。dyn Trait 亦可承载大小不同的实现,指针存放相应的运行时元数据。泛型默认 T: Sized,需要接收两类类型可考虑 fn f(x: &T)。

相关卡片 [[特征对象统一异构组件]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第19章 高级特性(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c19.xhtml`,块 0274) - 结论依据:原文用地址与长度解释 &str 为什么有固定尺寸,并导出把 DST 放在指针后的用法。 - 原始内容:So what do we do? In this case, you already know the answer: we make the types of s1 and s2 a &str rather than a str. Recall from “String Slices” on page 79 that the slice data structure just stores the starting position and the length of the slice.