1 //! # The Rust Standard Library
3 //! The Rust Standard Library is the foundation of portable Rust software, a
4 //! set of minimal and battle-tested shared abstractions for the [broader Rust
5 //! ecosystem][crates.io]. It offers core types, like [`Vec<T>`] and
6 //! [`Option<T>`], library-defined [operations on language
7 //! primitives](#primitives), [standard macros](#macros), [I/O] and
8 //! [multithreading], among [many other things][other].
10 //! `std` is available to all Rust crates by default. Therefore, the
11 //! standard library can be accessed in [`use`] statements through the path
12 //! `std`, as in [`use std::env`].
14 //! # How to read this documentation
16 //! If you already know the name of what you are looking for, the fastest way to
17 //! find it is to use the <a href="#" onclick="focusSearchBar();">search
18 //! bar</a> at the top of the page.
20 //! Otherwise, you may want to jump to one of these useful sections:
22 //! * [`std::*` modules](#modules)
23 //! * [Primitive types](#primitives)
24 //! * [Standard macros](#macros)
25 //! * [The Rust Prelude]
27 //! If this is your first time, the documentation for the standard library is
28 //! written to be casually perused. Clicking on interesting things should
29 //! generally lead you to interesting places. Still, there are important bits
30 //! you don't want to miss, so read on for a tour of the standard library and
31 //! its documentation!
33 //! Once you are familiar with the contents of the standard library you may
34 //! begin to find the verbosity of the prose distracting. At this stage in your
35 //! development you may want to press the `[-]` button near the top of the
36 //! page to collapse it into a more skimmable view.
38 //! While you are looking at that `[-]` button also notice the `source`
39 //! link. Rust's API documentation comes with the source code and you are
40 //! encouraged to read it. The standard library source is generally high
41 //! quality and a peek behind the curtains is often enlightening.
43 //! # What is in the standard library documentation?
45 //! First of all, The Rust Standard Library is divided into a number of focused
46 //! modules, [all listed further down this page](#modules). These modules are
47 //! the bedrock upon which all of Rust is forged, and they have mighty names
48 //! like [`std::slice`] and [`std::cmp`]. Modules' documentation typically
49 //! includes an overview of the module along with examples, and are a smart
50 //! place to start familiarizing yourself with the library.
52 //! Second, implicit methods on [primitive types] are documented here. This can
53 //! be a source of confusion for two reasons:
55 //! 1. While primitives are implemented by the compiler, the standard library
56 //! implements methods directly on the primitive types (and it is the only
57 //! library that does so), which are [documented in the section on
58 //! primitives](#primitives).
59 //! 2. The standard library exports many modules *with the same name as
60 //! primitive types*. These define additional items related to the primitive
61 //! type, but not the all-important methods.
63 //! So for example there is a [page for the primitive type
64 //! `i32`](primitive::i32) that lists all the methods that can be called on
65 //! 32-bit integers (very useful), and there is a [page for the module
66 //! `std::i32`] that documents the constant values [`MIN`] and [`MAX`] (rarely
69 //! Note the documentation for the primitives [`str`] and [`[T]`][prim@slice] (also
70 //! called 'slice'). Many method calls on [`String`] and [`Vec<T>`] are actually
71 //! calls to methods on [`str`] and [`[T]`][prim@slice] respectively, via [deref
72 //! coercions][deref-coercions].
74 //! Third, the standard library defines [The Rust Prelude], a small collection
75 //! of items - mostly traits - that are imported into every module of every
76 //! crate. The traits in the prelude are pervasive, making the prelude
77 //! documentation a good entry point to learning about the library.
79 //! And finally, the standard library exports a number of standard macros, and
80 //! [lists them on this page](#macros) (technically, not all of the standard
81 //! macros are defined by the standard library - some are defined by the
82 //! compiler - but they are documented here the same). Like the prelude, the
83 //! standard macros are imported by default into all crates.
85 //! # Contributing changes to the documentation
87 //! Check out the rust contribution guidelines [here](
88 //! https://rustc-dev-guide.rust-lang.org/contributing.html#writing-documentation).
89 //! The source for this documentation can be found on
90 //! [GitHub](https://github.com/rust-lang/rust).
91 //! To contribute changes, make sure you read the guidelines first, then submit
92 //! pull-requests for your suggested changes.
94 //! Contributions are appreciated! If you see a part of the docs that can be
95 //! improved, submit a PR, or chat with us first on [Discord][rust-discord]
98 //! # A Tour of The Rust Standard Library
100 //! The rest of this crate documentation is dedicated to pointing out notable
101 //! features of The Rust Standard Library.
103 //! ## Containers and collections
105 //! The [`option`] and [`result`] modules define optional and error-handling
106 //! types, [`Option<T>`] and [`Result<T, E>`]. The [`iter`] module defines
107 //! Rust's iterator trait, [`Iterator`], which works with the [`for`] loop to
108 //! access collections.
110 //! The standard library exposes three common ways to deal with contiguous
111 //! regions of memory:
113 //! * [`Vec<T>`] - A heap-allocated *vector* that is resizable at runtime.
114 //! * [`[T; N]`][prim@array] - An inline *array* with a fixed size at compile time.
115 //! * [`[T]`][prim@slice] - A dynamically sized *slice* into any other kind of contiguous
116 //! storage, whether heap-allocated or not.
118 //! Slices can only be handled through some kind of *pointer*, and as such come
119 //! in many flavors such as:
121 //! * `&[T]` - *shared slice*
122 //! * `&mut [T]` - *mutable slice*
123 //! * [`Box<[T]>`][owned slice] - *owned slice*
125 //! [`str`], a UTF-8 string slice, is a primitive type, and the standard library
126 //! defines many methods for it. Rust [`str`]s are typically accessed as
127 //! immutable references: `&str`. Use the owned [`String`] for building and
128 //! mutating strings.
130 //! For converting to strings use the [`format!`] macro, and for converting from
131 //! strings use the [`FromStr`] trait.
133 //! Data may be shared by placing it in a reference-counted box or the [`Rc`]
134 //! type, and if further contained in a [`Cell`] or [`RefCell`], may be mutated
135 //! as well as shared. Likewise, in a concurrent setting it is common to pair an
136 //! atomically-reference-counted box, [`Arc`], with a [`Mutex`] to get the same
139 //! The [`collections`] module defines maps, sets, linked lists and other
140 //! typical collection types, including the common [`HashMap<K, V>`].
142 //! ## Platform abstractions and I/O
144 //! Besides basic data types, the standard library is largely concerned with
145 //! abstracting over differences in common platforms, most notably Windows and
146 //! Unix derivatives.
148 //! Common types of I/O, including [files], [TCP], [UDP], are defined in the
149 //! [`io`], [`fs`], and [`net`] modules.
151 //! The [`thread`] module contains Rust's threading abstractions. [`sync`]
152 //! contains further primitive shared memory types, including [`atomic`] and
153 //! [`mpsc`], which contains the channel types for message passing.
156 //! [`MIN`]: i32::MIN
157 //! [`MAX`]: i32::MAX
158 //! [page for the module `std::i32`]: crate::i32
159 //! [TCP]: net::TcpStream
160 //! [The Rust Prelude]: prelude
161 //! [UDP]: net::UdpSocket
162 //! [`Arc`]: sync::Arc
163 //! [owned slice]: boxed
164 //! [`Cell`]: cell::Cell
165 //! [`FromStr`]: str::FromStr
166 //! [`HashMap<K, V>`]: collections::HashMap
167 //! [`Mutex`]: sync::Mutex
168 //! [`Option<T>`]: option::Option
170 //! [`RefCell`]: cell::RefCell
171 //! [`Result<T, E>`]: result::Result
172 //! [`Vec<T>`]: vec::Vec
173 //! [`atomic`]: sync::atomic
174 //! [`for`]: ../book/ch03-05-control-flow.html#looping-through-a-collection-with-for
175 //! [`str`]: prim@str
176 //! [`mpsc`]: sync::mpsc
177 //! [`std::cmp`]: cmp
178 //! [`std::slice`]: mod@slice
179 //! [`use std::env`]: env/index.html
180 //! [`use`]: ../book/ch07-02-defining-modules-to-control-scope-and-privacy.html
181 //! [crates.io]: https://crates.io
182 //! [deref-coercions]: ../book/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods
183 //! [files]: fs::File
184 //! [multithreading]: thread
185 //! [other]: #what-is-in-the-standard-library-documentation
186 //! [primitive types]: ../book/ch03-02-data-types.html
187 //! [rust-discord]: https://discord.gg/rust-lang
188 //! [array]: prim@array
189 //! [slice]: prim@slice
190 #![cfg_attr(not(feature = "restricted-std"), stable(feature = "rust1", since = "1.0.0"))]
191 #![cfg_attr(feature = "restricted-std", unstable(feature = "restricted_std", issue = "none"))]
193 html_playground_url = "https://play.rust-lang.org/",
194 issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
195 test(no_crate_inject, attr(deny(warnings))),
196 test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
200 not(any(test, bootstrap)),
201 no_global_oom_handling,
202 not(no_global_oom_handling)
204 // Don't link to std. We are std.
206 #![warn(deprecated_in_future)]
207 #![warn(missing_docs)]
208 #![warn(missing_debug_implementations)]
209 #![allow(explicit_outlives_requirements)]
210 #![allow(unused_lifetimes)]
211 // Tell the compiler to link to either panic_abort or panic_unwind
212 #![needs_panic_runtime]
213 // Ensure that std can be linked against panic_abort despite compiled with `-C panic=unwind`
214 #![deny(ffi_unwind_calls)]
215 // std may use features in a platform-specific way
216 #![allow(unused_features)]
217 #![cfg_attr(test, feature(internal_output_capture, print_internals, update_panic_count, rt))]
219 all(target_vendor = "fortanix", target_env = "sgx"),
220 feature(slice_index_methods, coerce_unsized, sgx_platform)
222 #![deny(rustc::existing_doc_keyword)]
224 // Language features:
225 #![feature(alloc_error_handler)]
226 #![feature(allocator_internals)]
227 #![feature(allow_internal_unsafe)]
228 #![feature(allow_internal_unstable)]
229 #![feature(box_syntax)]
230 #![feature(c_unwind)]
231 #![feature(cfg_target_thread_local)]
232 #![feature(concat_idents)]
233 #![feature(const_mut_refs)]
234 #![feature(const_trait_impl)]
235 #![feature(decl_macro)]
236 #![feature(deprecated_suggestion)]
238 #![feature(doc_cfg_hide)]
239 #![feature(doc_masked)]
240 #![feature(doc_notable_trait)]
241 #![feature(dropck_eyepatch)]
242 #![feature(exhaustive_patterns)]
243 #![feature(intra_doc_pointers)]
244 #![feature(label_break_value)]
245 #![feature(lang_items)]
246 #![feature(let_else)]
248 #![feature(link_cfg)]
249 #![feature(min_specialization)]
250 #![feature(must_not_suspend)]
251 #![feature(needs_panic_runtime)]
252 #![feature(negative_impls)]
253 #![feature(never_type)]
254 #![feature(platform_intrinsics)]
255 #![feature(prelude_import)]
256 #![feature(rustc_attrs)]
257 #![feature(rustdoc_internals)]
258 #![feature(staged_api)]
259 #![feature(thread_local)]
260 #![feature(try_blocks)]
261 #![feature(utf8_chunks)]
263 // Library features (core):
264 #![feature(array_error_internals)]
265 #![feature(atomic_mut_ptr)]
266 #![feature(char_error_internals)]
267 #![feature(char_internals)]
268 #![feature(core_intrinsics)]
269 #![feature(cstr_from_bytes_until_nul)]
270 #![feature(cstr_internals)]
271 #![feature(duration_checked_float)]
272 #![feature(duration_constants)]
273 #![feature(exact_size_is_empty)]
274 #![feature(exclusive_wrapper)]
275 #![feature(extend_one)]
276 #![feature(float_minimum_maximum)]
277 #![feature(hasher_prefixfree_extras)]
278 #![feature(hashmap_internals)]
279 #![feature(int_error_internals)]
280 #![feature(is_some_with)]
281 #![feature(maybe_uninit_slice)]
282 #![feature(maybe_uninit_write_slice)]
283 #![feature(mixed_integer_ops)]
284 #![feature(nonnull_slice_from_raw_parts)]
285 #![feature(panic_can_unwind)]
286 #![feature(panic_info_message)]
287 #![feature(panic_internals)]
288 #![feature(portable_simd)]
289 #![feature(prelude_2024)]
290 #![feature(provide_any)]
291 #![feature(ptr_as_uninit)]
292 #![feature(raw_os_nonzero)]
293 #![feature(slice_internals)]
294 #![feature(slice_ptr_get)]
295 #![feature(std_internals)]
296 #![feature(str_internals)]
297 #![feature(strict_provenance)]
298 #![feature(maybe_uninit_uninit_array)]
299 #![feature(const_maybe_uninit_uninit_array)]
301 // Library features (alloc):
302 #![feature(alloc_layout_extra)]
303 #![feature(allocator_api)]
304 #![feature(get_mut_unchecked)]
305 #![feature(map_try_insert)]
306 #![feature(new_uninit)]
307 #![feature(thin_box)]
308 #![feature(try_reserve_kind)]
309 #![feature(vec_into_raw_parts)]
310 #![feature(slice_concat_trait)]
312 // Library features (unwind):
313 #![feature(panic_unwind)]
315 // Only for re-exporting:
316 #![feature(assert_matches)]
317 #![feature(async_iterator)]
318 #![feature(c_variadic)]
319 #![feature(cfg_accessible)]
320 #![feature(cfg_eval)]
321 #![feature(concat_bytes)]
322 #![feature(const_format_args)]
323 #![feature(core_panic)]
324 #![feature(custom_test_frameworks)]
325 #![feature(edition_panic)]
326 #![feature(format_args_nl)]
327 #![feature(log_syntax)]
328 #![feature(once_cell)]
329 #![feature(saturating_int_impl)]
332 #![feature(trace_macros)]
334 // Only used in tests/benchmarks:
335 #![feature(bench_black_box)]
337 // Only for const-ness:
338 #![feature(const_io_structs)]
339 #![feature(const_ip)]
340 #![feature(const_ipv4)]
341 #![feature(const_ipv6)]
342 #![feature(const_socketaddr)]
343 #![feature(thread_local_internals)]
345 #![default_lib_allocator]
347 // Explicitly import the prelude. The compiler uses this same unstable attribute
348 // to import the prelude implicitly when building crates that depend on std.
351 use prelude::rust_2021::*;
353 // Access to Bencher, etc.
357 #[allow(unused_imports)] // macros from `alloc` are not used on all platforms
359 extern crate alloc as alloc_crate;
361 #[allow(unused_extern_crates)]
364 // We always need an unwinder currently for backtraces
366 #[allow(unused_extern_crates)]
370 #[allow(unused_extern_crates)]
371 #[cfg(feature = "miniz_oxide")]
372 extern crate miniz_oxide;
374 // During testing, this crate is not actually the "real" std library, but rather
375 // it links to the real std library, which was compiled from this same source
376 // code. So any lang items std defines are conditionally excluded (or else they
377 // would generate duplicate lang item errors), and any globals it defines are
378 // _not_ the globals used by "real" std. So this import, defined only during
379 // testing gives test-std access to real-std lang items and globals. See #2912
381 extern crate std as realstd;
383 // The standard macros that are not built-in to the compiler.
387 // The runtime entry point and a few unstable public functions used by the
395 // Public module declarations and re-exports
396 #[stable(feature = "rust1", since = "1.0.0")]
397 pub use alloc_crate::borrow;
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub use alloc_crate::boxed;
400 #[stable(feature = "rust1", since = "1.0.0")]
401 pub use alloc_crate::fmt;
402 #[stable(feature = "rust1", since = "1.0.0")]
403 pub use alloc_crate::format;
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub use alloc_crate::rc;
406 #[stable(feature = "rust1", since = "1.0.0")]
407 pub use alloc_crate::slice;
408 #[stable(feature = "rust1", since = "1.0.0")]
409 pub use alloc_crate::str;
410 #[stable(feature = "rust1", since = "1.0.0")]
411 pub use alloc_crate::string;
412 #[stable(feature = "rust1", since = "1.0.0")]
413 pub use alloc_crate::vec;
414 #[stable(feature = "rust1", since = "1.0.0")]
416 #[stable(feature = "core_array", since = "1.36.0")]
418 #[unstable(feature = "async_iterator", issue = "79024")]
419 pub use core::async_iter;
420 #[stable(feature = "rust1", since = "1.0.0")]
422 #[stable(feature = "rust1", since = "1.0.0")]
424 #[stable(feature = "rust1", since = "1.0.0")]
426 #[stable(feature = "rust1", since = "1.0.0")]
428 #[stable(feature = "rust1", since = "1.0.0")]
429 pub use core::convert;
430 #[stable(feature = "rust1", since = "1.0.0")]
431 pub use core::default;
432 #[stable(feature = "futures_api", since = "1.36.0")]
433 pub use core::future;
434 #[stable(feature = "rust1", since = "1.0.0")]
436 #[stable(feature = "core_hint", since = "1.27.0")]
438 #[stable(feature = "i128", since = "1.26.0")]
439 #[allow(deprecated, deprecated_in_future)]
441 #[stable(feature = "rust1", since = "1.0.0")]
442 #[allow(deprecated, deprecated_in_future)]
444 #[stable(feature = "rust1", since = "1.0.0")]
445 #[allow(deprecated, deprecated_in_future)]
447 #[stable(feature = "rust1", since = "1.0.0")]
448 #[allow(deprecated, deprecated_in_future)]
450 #[stable(feature = "rust1", since = "1.0.0")]
451 #[allow(deprecated, deprecated_in_future)]
453 #[stable(feature = "rust1", since = "1.0.0")]
454 pub use core::intrinsics;
455 #[stable(feature = "rust1", since = "1.0.0")]
456 #[allow(deprecated, deprecated_in_future)]
458 #[stable(feature = "rust1", since = "1.0.0")]
460 #[stable(feature = "rust1", since = "1.0.0")]
461 pub use core::marker;
462 #[stable(feature = "rust1", since = "1.0.0")]
464 #[stable(feature = "rust1", since = "1.0.0")]
466 #[stable(feature = "rust1", since = "1.0.0")]
467 pub use core::option;
468 #[stable(feature = "pin", since = "1.33.0")]
470 #[stable(feature = "rust1", since = "1.0.0")]
472 #[stable(feature = "rust1", since = "1.0.0")]
473 pub use core::result;
474 #[stable(feature = "i128", since = "1.26.0")]
475 #[allow(deprecated, deprecated_in_future)]
477 #[stable(feature = "rust1", since = "1.0.0")]
478 #[allow(deprecated, deprecated_in_future)]
480 #[stable(feature = "rust1", since = "1.0.0")]
481 #[allow(deprecated, deprecated_in_future)]
483 #[stable(feature = "rust1", since = "1.0.0")]
484 #[allow(deprecated, deprecated_in_future)]
486 #[stable(feature = "rust1", since = "1.0.0")]
487 #[allow(deprecated, deprecated_in_future)]
489 #[stable(feature = "rust1", since = "1.0.0")]
490 #[allow(deprecated, deprecated_in_future)]
515 #[unstable(feature = "once_cell", issue = "74465")]
518 // Pull in `std_float` crate into libstd. The contents of
519 // `std_float` are in a different repository: rust-lang/portable-simd.
520 #[path = "../../portable-simd/crates/std_float/src/lib.rs"]
521 #[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn, unused_unsafe)]
522 #[allow(rustdoc::bare_urls)]
523 #[unstable(feature = "portable_simd", issue = "86656")]
526 #[doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
527 #[unstable(feature = "portable_simd", issue = "86656")]
530 pub use crate::std_float::StdFloat;
532 pub use core::simd::*;
535 #[stable(feature = "futures_api", since = "1.36.0")]
537 //! Types and Traits for working with asynchronous tasks.
540 #[stable(feature = "futures_api", since = "1.36.0")]
541 pub use core::task::*;
544 #[stable(feature = "wake_trait", since = "1.51.0")]
545 pub use alloc::task::*;
548 #[doc = include_str!("../../stdarch/crates/core_arch/src/core_arch_docs.md")]
549 #[stable(feature = "simd_arch", since = "1.27.0")]
551 #[stable(feature = "simd_arch", since = "1.27.0")]
552 // The `no_inline`-attribute is required to make the documentation of all
553 // targets available.
554 // See https://github.com/rust-lang/rust/pull/57808#issuecomment-457390549 for
556 #[doc(no_inline)] // Note (#82861): required for correct documentation
557 pub use core::arch::*;
559 #[stable(feature = "simd_aarch64", since = "1.60.0")]
560 pub use std_detect::is_aarch64_feature_detected;
561 #[stable(feature = "simd_x86", since = "1.27.0")]
562 pub use std_detect::is_x86_feature_detected;
563 #[unstable(feature = "stdsimd", issue = "48556")]
564 pub use std_detect::{
565 is_arm_feature_detected, is_mips64_feature_detected, is_mips_feature_detected,
566 is_powerpc64_feature_detected, is_powerpc_feature_detected, is_riscv_feature_detected,
570 // This was stabilized in the crate root so we have to keep it there.
571 #[stable(feature = "simd_x86", since = "1.27.0")]
572 pub use std_detect::is_x86_feature_detected;
574 // Platform-abstraction modules
580 // Private support modules
583 #[path = "../../backtrace/src/lib.rs"]
584 #[allow(dead_code, unused_attributes)]
587 // Re-export macros defined in libcore.
588 #[stable(feature = "rust1", since = "1.0.0")]
589 #[allow(deprecated, deprecated_in_future)]
591 assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, matches, todo, r#try,
592 unimplemented, unreachable, write, writeln,
595 // Re-export built-in macros defined through libcore.
596 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
599 assert, assert_matches, cfg, column, compile_error, concat, concat_idents, const_format_args,
600 env, file, format_args, format_args_nl, include, include_bytes, include_str, line, log_syntax,
601 module_path, option_env, stringify, trace_macros,
605 feature = "concat_bytes",
607 reason = "`concat_bytes` is not stable enough for use and is subject to change"
609 pub use core::concat_bytes;
611 #[stable(feature = "core_primitive", since = "1.43.0")]
612 pub use core::primitive;
614 // Include a number of private modules that exist solely to provide
615 // the rustdoc documentation for primitive types. Using `include!`
616 // because rustdoc only looks for these modules at the crate level.
617 include!("primitive_docs.rs");
619 // Include a number of private modules that exist solely to provide
620 // the rustdoc documentation for the existing keywords. Using `include!`
621 // because rustdoc only looks for these modules at the crate level.
622 include!("keyword_docs.rs");
624 // This is required to avoid an unstable error when `restricted-std` is not
625 // enabled. The use of #![feature(restricted_std)] in rustc-std-workspace-std
626 // is unconditional, so the unstable feature needs to be defined somewhere.
627 #[unstable(feature = "restricted_std", issue = "none")]
628 mod __restricted_std_workaround {}
631 /// This trait being unreachable from outside the crate
632 /// prevents outside implementations of our extension traits.
633 /// This allows adding more trait methods in the future.
634 #[unstable(feature = "sealed", issue = "none")]