在 MacOS 上提速 Rust 编译
Contents
MacOS 下做 Rust 开发时,如果你突然发现“编译怎么慢得离谱”,很可能不是项目本身的问题,而是系统安全机制在拖慢每一次新产生的可执行文件启动。
TL;DR:编译过程中会生成并运行大量临时可执行文件(build script、proc-macro 等),而 macOS 的 XProtect/执行校验会在“首次运行/文件变化”时触发扫描。把你常用的终端/IDE 加入 系统设置 → 隐私与安全性 → 开发者工具(Developer Tools),可以显著减少这类验证带来的阻塞。
为什么会慢:XProtect 的触发点
XProtect 会在这些时机检测恶意内容:
- App 首次启动时
- App 变化时(在文件系统中)
- XProtect 签名更新时
换句话说:每个新生成的可执行文件在首次运行时都可能触发验证。对于“从互联网下载的 App”这很合理;但对本地开发来说就很不友好——Rust 的编译链路会频繁生成并运行中间产物(例如 build script、测试二进制、增量编译产物),于是开销会被放大。
解决:把开发工具加入 Developer Tools 白名单
在 Zed 的 macOS 开发文档 里提到了这个问题,并给出了一种通用做法:开启终端的 developer-mode,并在系统“Developer Tools”里把常用终端/IDE 加进去。
How to add your terminal to Developer Tools
- Run
sudo spctl developer-mode enable-terminalin your terminal.- Go to System Preferences, and then to Security & Privacy.
- Under the Privacy tab, an item called
Developer Toolsshould be present. Navigate to it.- Ensure that your terminal is listed and enabled. If you’re using a third-party terminal like iTerm, be sure to add it to the list. (You may have to click the lock in the bottom-left corner and authenticate.)
- Restart your terminal.

按这个方法把你日常用的工具(Terminal/iTerm/VS Code/Zed 等)加入 Developer Tools 后,编译链路里大量“新可执行文件的首次运行”就不会被反复拖慢,体感改善非常明显。