鉴于中文互联网关于rust prelude相关内容较少,专门讲讲prelude的内容
prelude模块的引入,定义在/std/src/os/prelude/文件夹下 prelude模块分为根据edition以及第一个正式引入版本v1
#![stable(feature = "rust1", since = "1.0.0")]
pub mod v1;
/// The 2015 version of the prelude of The Rust Standard Library.
///
/// See the [module-level documentation](self) for more.
#[stable(feature = "prelude_2015", since = "1.55.0")]
pub mod rust_2015 {
#[stable(feature = "prelude_2015", since = "1.55.0")]
#[doc(no_inline)]
pub use super::v1::*;
}
/// The 2018 version of the prelude of The Rust Standard Library.
///
/// See the [module-level documentation](self) for more.
#[stable(feature = "prelude_2018", since = "1.55.0")]
pub mod rust_2018 {
#[stable(feature = "prelude_2018", since = "1.55.0")]
#[doc(no_inline)]
pub use super::v1::*;
}
/// The 2021 version of the prelude of The Rust Standard Library.
///
/// See the [module-level documentation](self) for more.
#[stable(feature = "prelude_2021", since = "1.55.0")]
pub mod rust_2021 {
#[stable(feature = "prelude_2021", since = "1.55.0")]
#[doc(no_inline)]
pub use super::v1::*;
#[stable(feature = "prelude_2021", since = "1.55.0")]
#[doc(no_inline)]
pub use core::prelude::rust_2021::*;
}
/// The 2024 version of the prelude of The Rust Standard Library.
///
/// See the [module-level documentation](self) for more.
#[unstable(feature = "prelude_2024", issue = "none")]
pub mod rust_2024 {
#[unstable(feature = "prelude_2024", issue = "none")]
#[doc(no_inline)]
pub use super::v1::*;
#[unstable(feature = "prelude_2024", issue = "none")]
#[doc(no_inline)]
pub use core::prelude::rust_2024::*;
}
可以看到15、18版都只是单纯引入了v1这个正式版本,而现在正在使用的2021版,则引入了专属于2021版的单独引入,这种设计也是为了向下兼容性,另外,我们可以看到,rust已经在准备开发2024的prelude模块
根据注释我们可以看到详细的prelude内容,另外在2021版引入了
TryFrom TryInto以及FromTerator
如果对这部分感兴趣的朋友可以直接步入v1这个模块详细查看
也可以通过官方std文档
rustwiki.org/zh-CN/std/p…
进行查看