std::prelude - Rust (original) (raw)
Module prelude
1.0.0 · Source
Expand description
Rust comes with a variety of things in its standard library. However, if you had to manually import every single thing that you used, it would be very verbose. But importing a lot of things that a program never uses isn’t good either. A balance needs to be struck.
The prelude is the list of things that Rust automatically imports into every Rust program. It’s kept as small as possible, and is focused on things, particularly traits, which are used in almost every single Rust program.
§Other preludes
Preludes can be seen as a pattern to make using multiple types more convenient. As such, you’ll find other preludes in the standard library, such as std::io::prelude. Various libraries in the Rust ecosystem may also define their own preludes.
The difference between ‘the prelude’ and these other preludes is that they are not automatically use
’d, and must be imported manually. This is still easier than importing all of their constituent components.
§Prelude contents
The items included in the prelude depend on the edition of the crate. The first version of the prelude is used in Rust 2015 and Rust 2018, and lives in std::prelude::v1.std::prelude::rust_2015 and std::prelude::rust_2018 re-export this prelude. It re-exports the following:
[std::marker](../marker/index.html "mod std::marker")::{[Copy](../marker/trait.Copy.html "trait std:📑:Copy"), [Send](../marker/trait.Send.html "trait std:📑:Send"), [Sized](../marker/trait.Sized.html "trait std:📑:Sized"), [Sync](../marker/trait.Sync.html "trait std:📑:Sync"), [Unpin](../marker/trait.Unpin.html "trait std:📑:Unpin")}
, marker traits that indicate fundamental properties of types.[std::ops](../ops/index.html "mod std::ops")::{[Fn](../ops/trait.Fn.html "trait std::ops::Fn"), [FnMut](../ops/trait.FnMut.html "trait std::ops::FnMut"), [FnOnce](../ops/trait.FnOnce.html "trait std::ops::FnOnce")}
, and their analogous async traits,[std::ops](../ops/index.html "mod std::ops")::{[AsyncFn](../ops/trait.AsyncFn.html "trait std::ops::AsyncFn"), [AsyncFnMut](../ops/trait.AsyncFnMut.html "trait std::ops::AsyncFnMut"), [AsyncFnOnce](../ops/trait.AsyncFnOnce.html "trait std::ops::AsyncFnOnce")}
.[std::ops](../ops/index.html "mod std::ops")::[Drop](../ops/trait.Drop.html "trait std::ops::Drop")
, for implementing destructors.[std::mem](../mem/index.html "mod std::mem")::[drop](../mem/fn.drop.html "fn std::mem::drop")
, a convenience function for explicitly dropping a value.[std::mem](../mem/index.html "mod std::mem")::{[size_of](../mem/fn.size%5Fof.html "fn std::mem::size_of"), [size_of_val](../mem/fn.size%5Fof%5Fval.html "fn std::mem::size_of_val")}
, to get the size of a type or value.[std::mem](../mem/index.html "mod std::mem")::{[align_of](../mem/fn.align%5Fof.html "fn std::mem::align_of"), [align_of_val](../mem/fn.align%5Fof%5Fval.html "fn std::mem::align_of_val")}
, to get the alignment of a type or value.[std::boxed](../boxed/index.html "mod std::boxed")::[Box](../boxed/struct.Box.html "struct std::boxed::Box")
, a way to allocate values on the heap.[std::borrow](../borrow/index.html "mod std::borrow")::[ToOwned](../borrow/trait.ToOwned.html "trait std::borrow::ToOwned")
, the conversion trait that definesto_owned, the generic method for creating an owned type from a borrowed type.[std::clone](../clone/index.html "mod std::clone")::[Clone](../clone/trait.Clone.html "trait std::clone::Clone")
, the ubiquitous trait that definesclone, the method for producing a copy of a value.[std::cmp](../cmp/index.html "mod std::cmp")::{[PartialEq](../cmp/trait.PartialEq.html "trait std::cmp::PartialEq"), [PartialOrd](../cmp/trait.PartialOrd.html "trait std::cmp::PartialOrd"), [Eq](../cmp/trait.Eq.html "trait std::cmp::Eq"), [Ord](../cmp/trait.Ord.html "trait std::cmp::Ord")}
, the comparison traits, which implement the comparison operators and are often seen in trait bounds.[std::convert](../convert/index.html "mod std::convert")::{[AsRef](../convert/trait.AsRef.html "trait std::convert::AsRef"), [AsMut](../convert/trait.AsMut.html "trait std::convert::AsMut"), [Into](../convert/trait.Into.html "trait std::convert::Into"), [From](../convert/trait.From.html "trait std::convert::From")}
, generic conversions, used by savvy API authors to create overloaded methods.[std::default](../default/index.html "mod std::default")::[Default](../default/trait.Default.html "trait std::default::Default")
, types that have default values.[std::iter](../iter/index.html "mod std::iter")::{[Iterator](../iter/trait.Iterator.html "trait std::iter::Iterator"), [Extend](../iter/trait.Extend.html "trait std::iter::Extend"), [IntoIterator](../iter/trait.IntoIterator.html "trait std::iter::IntoIterator"), [DoubleEndedIterator](../iter/trait.DoubleEndedIterator.html "trait std::iter::DoubleEndedIterator"), [ExactSizeIterator](../iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator")}
, iterators of various kinds.[std::option](../option/index.html "mod std::option")::[Option](../option/enum.Option.html "enum std::option::Option")::{[self](../option/enum.Option.html "enum std::option::Option"), [Some](../option/enum.Option.html#variant.Some "variant std::option::Option::Some"), [None](../option/enum.Option.html#variant.None "variant std::option::Option::None")}
, a type which expresses the presence or absence of a value. This type is so commonly used, its variants are also exported.[std::result](../result/index.html "mod std::result")::[Result](../result/enum.Result.html "enum std::result::Result")::{[self](../result/enum.Result.html "enum std::result::Result"), [Ok](../result/enum.Result.html#variant.Ok "variant std::result::Result::Ok"), [Err](../result/enum.Result.html#variant.Err "variant std::result::Result::Err")}
, a type for functions that may succeed or fail. Like Option, its variants are exported as well.[std::string](../string/index.html "mod std::string")::{[String](../string/struct.String.html "struct std:🧵:String"), [ToString](../string/trait.ToString.html "trait std:🧵:ToString")}
, heap-allocated strings.[std::vec](../vec/index.html "mod std::vec")::[Vec](../vec/struct.Vec.html "struct std::vec::Vec")
, a growable, heap-allocated vector.
The prelude used in Rust 2021, std::prelude::rust_2021, includes all of the above, and in addition re-exports:
[std::convert](../convert/index.html "mod std::convert")::{[TryFrom](../convert/trait.TryFrom.html "trait std::convert::TryFrom"), [TryInto](../convert/trait.TryInto.html "trait std::convert::TryInto")}
.[std::iter](../iter/index.html "mod std::iter")::[FromIterator](../iter/trait.FromIterator.html "trait std::iter::FromIterator")
.
The prelude used in Rust 2024, std::prelude::rust_2024, includes all of the above, and in addition re-exports:
[std::future](../future/index.html "mod std::future")::{[Future](../future/trait.Future.html "trait std::future::Future"), [IntoFuture](../future/trait.IntoFuture.html "trait std::future::IntoFuture")}
.
The 2015 version of the prelude of The Rust Standard Library.
The 2018 version of the prelude of The Rust Standard Library.
The 2021 version of the prelude of The Rust Standard Library.
The 2024 version of the prelude of The Rust Standard Library.
The first version of the prelude of The Rust Standard Library.