项目简介
Rust 编程语言的官方源码仓库,包含 rustc 编译器、标准库与文档。Rust 注重性能与内存安全,无需垃圾回收即可保证线程安全,并提供 Cargo 包管理器、rustfmt 格式化工具与 Clippy 静态检查等完整工具链。
README 预览
\n \n \n \n \n \n\n[Website][Rust] | [Getting started] | [Learn] | [Documentation] | [Contributing]\n\n\nThis is the main source code repository for [Rust]. It contains the compiler,\nstandard library, and documentation.\n\n[Rust]: https://www.rust-lang.org/\n[Getting Started]: https://www.rust-lang.org/learn/get-started\n[Learn]: https://www.rust-lang.org/learn\n[Documentation]: https://www.rust-lang.org/learn#learn-use\n[Contributing]: CONTRIBUTING.md\n\n## Why Rust?\n\n- **Performance:** Fast and memory-efficient, suitable for critical services, embedded devices, and easily integrated with other languages.\n\n- **Reliability:** Our rich type system and ownership model ensure memory and thread safety, reducing bugs at compile-time.\n\n- **Productivity:** Comprehensive documentation, a compiler committed to providing great diagnostics, and advanced tooling including package manager and build tool ([Cargo]), auto-formatter ([rustfmt]), linter ([Clippy]) and editor support ([rust-analyzer]).\n\n[Cargo]: https://github.com/rust-lang/cargo\n[rustfmt]: https://github.com/rust-lang/rustfmt\n[Clippy]: https://github.com/rust-lang/rust-clippy\n[rust-analyzer]: https://github.com/rust-lang/rust-analyzer\n\n## Quick Start\n\nRead ["Installation"] from [The Book].\n\n["Installation"]: https://doc.rust-lang.org/book/ch01-01-installation.html\n[The Book]: https://doc.rust-lang.org/book/index.html\n\n## Installing from Source\n\nIf you really want to install from source (though this is not recommended), see\n[INSTALL.md](INSTALL.md).\n\n## Getting Help\n\nSee https://www.rust-lang.org/community for a list of chat platforms and forums.\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md).\n\nFor a detailed explanation of the compiler's architecture and how to begin contributing, see the [rustc-dev-guide](https://rustc-dev-guide.rust-lang.org/).\n\n## License\n\nRust is primarily distributed under the terms of both the MIT license and the\nApache License (Versio
常见问题 (5)
故障排除如何修复 Rust 错误 "internal compiler error: encountered incremental compilation error with evaluate_obligation"?
这是增量编译缓存损坏导致的 ICE,而不是你代码中的 bug。通过清除增量缓存来修复:
1. 运行 cargo clean -p <crate_name>(例如 cargo clean -p jmap)仅清除受影响的 crate,或运行完整的 cargo clean 清除所有构建产物。
2. 使用 cargo build 重新编译。
如果错误反复出现,可以通过设置环境变量 CARGO_INCREMENTAL=0,或在 .cargo/config.toml 中添加 [build] incremental = false 来完全禁用增量编译。
由于这是编译器的 bug,你还应该前往 https://github.com/rust-lang/rust/issues 报告,并附上最小复现示例(理想情况下是原始代码加上在重新编译时触发 ICE 的补丁),包括你的 rustc --version 输出和错误中的 fingerprint 哈希。
故障排除为什么我的 Rust 程序在 opt-level 1+ 下使用 checked_div().unwrap_unchecked() 时会因 SIGFPE(除零)而崩溃,以及如何解决这个问题?
这是一个已知的 rustc/LLVM 错误编译(miscompilation)bug(rust-lang/rust #164431,影响 ~1.87 至至少 nightly 1.101 的版本)。优化器会将 checked_div(...).unwrap_unchecked() 降级为一条裸的 div 指令,在跨函数内联之后,该指令可能接收到零除数,从而在 -C opt-level=1 及以上优化级别产生 SIGFPE(退出码 136),而 -C opt-level=0 和 Miri 则运行正常。在该 bug 修复之前的变通方法:1)将 checked_div(...).unwrap_unchecked() 替换为 wrapping_div(...) —— 崩溃消失。2)为被内联到调用者中的函数添加 #[inline(never)] —— 这会阻止触发该 bug 的跨函数内联。3)作为最后的手段,使用 -C opt-level=0 进行编译。请注意,该 bug 还对地址布局敏感:使用默认的 PIE 构建可以复现,但使用非 PIE 构建则无法可靠复现。
故障排除如何修复在 riscv64 上编译 libc crate 时出现的 'cannot find type c_char in this scope' 错误?
此 E0425 错误的发生原因是过时的 libc 版本(例如 0.2.43)缺少针对 riscv64 目标的核心 C 类型(如 c_char)的定义。修复方法是将 libc 升级到 0.2.190 或更高版本。如果 libc 被某个依赖项的 Cargo.lock 锁定(例如 cargotest 中使用的 xsv crate),请在受影响 crate 的目录中运行 cargo update -p libc,或者手动在 Cargo.lock/Cargo.toml 中提升 libc 版本,然后重新构建。不要按照编译器的建议导入 std::ffi::c_char —— 缺失的定义是版本问题,而非导入问题。
故障排除如何修复使用 inherent_associated_types 时出现的 Rust ICE "item_bounds called on DefId" 错误?
此内部编译器错误影响 rustc 1.98.0-nightly (485ec3fbc 2026-06-10),在编译递归的固有关联类型(例如 type Assoc = &'a Foo<'a>::Assoc;)并使用 -Znext-solver=globally -Zassumptions-on-binders 标志时触发。修复方法:通过 rustup update nightly 升级到 nightly 1.99.0 (ba28ff76f 2026-08-13) 或更新版本,在这些版本中该问题不再复现。请注意,inherent_associated_types 是一个不完整的不稳定特性,已知会导致编译器崩溃——请避免在 stable/beta 版本上使用它。此外,触发该错误的代码本身就是无效的(自引用关联类型会产生 E0271 类型不匹配错误),因此将关联类型改写为不引用自身,也可以在受影响的版本上解决该问题。
故障排除如何修复升级到 Rust 1.99 beta 后出现的 'type annotations needed' 错误?
这是 Rust 1.99 beta 中一个已知的类型推断回归问题(经二分定位到 PR #158447,与 issue #161913 重复),影响将闭包传递给泛型构造函数(例如 Foo::new(&1u8, |input| *input))的代码——这类代码此前可以正常推断类型。修复方法:升级到 Rust 1.100.0-beta.7 或更高版本,该回归问题在这些版本中不再复现——运行 rustup update,或使用 rustup default stable 切换通道。如果必须停留在 1.99 beta,变通方法是:为闭包参数或泛型参数添加显式类型注解,例如 |input: &u8| *input;或者在绑定上指定完整类型,例如 let x: Foo<&u8, u8> = ...。