Lobsters | 原文链接 | 2026-09-06 收录

Mojo 的「薛定谔指针」:raw Pointer 为何能安全与不安全并存

来源: melodyogonna.substack.com — 2026-09-04

概述

一篇面向初学者的 Mojo 指针类型简析。Mojo 的 Pointer 之所以「可安全可不安全」,取决于对它做的操作,根子在其所有权与生命周期模型:唯一所有权、ASAP 销毁和 origins(来源)三件套在编译期保证指针不悬空也不为空。携带 origin 的指针默认完全安全,编译器据此做别名排他与生命周期分析;去掉 origin 后则退回手动模式,安全与否交给使用者。

核心要点

金句

It is guaranteed to Point to an existing, fully initialised value that can never be null. For as long as the pointer is in use, the referenced value will remain alive.
返回 Lobsters 首页