知识卡片
内部可变性延后借用检查
RefCell<T> 单独持有 T,允许通过共享容器引用请求对内容的独占访问。借用仍受互斥约束,由安全 API 在运行时检查。它适合静态分析表达不了的单线程借用关系,代价是运行时开销及晚发现的冲突。优先选能在编译期完成检查的表达。
相关卡片 [[Rc 共享同一份所有权]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 智能指针 RefCell<T> and the Interior Mutability Pattern · 查看原书依据
With references and Box<T>, the borrowing rules’ invariants are enforced at compile time. With RefCell<T>, these invariants are enforced at runtime. With references, if you break these rules, you’ll get a compiler error. With RefCell<T>, if you break these rules, your program will panic and exit.
参考来源
- 位置:《The Rust Programming Language(第2版)》第15章 智能指针(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c15.xhtml`,块 0226)
- 结论依据:原文对照 Box 与 RefCell 的检查时点和违例结果。
- 原始内容:With references and Box<T>, the borrowing rules’ invariants are enforced at compile time. With RefCell<T>, these invariants are enforced at runtime. With references, if you break these rules, you’ll get a compiler error. With RefCell<T>, if you break these rules, your program will panic and exit.