1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! # The Rust Standard Library
13 //! The Rust Standard Library is the foundation of portable Rust software, a
14 //! set of minimal and battle-tested shared abstractions for the [broader Rust
15 //! ecosystem][crates.io]. It offers core types, like [`Vec<T>`] and
16 //! [`Option<T>`], library-defined [operations on language
17 //! primitives](#primitives), [standard macros](#macros), [I/O] and
18 //! [multithreading], among [many other things][other].
20 //! `std` is available to all Rust crates by default, just as if each one
21 //! contained an `extern crate std;` import at the [crate root]. Therefore the
22 //! standard library can be accessed in [`use`] statements through the path
23 //! `std`, as in [`use std::env`], or in expressions through the absolute path
24 //! `::std`, as in [`::std::env::args`].
26 //! # How to read this documentation
28 //! If you already know the name of what you are looking for, the fastest way to
29 //! find it is to use the <a href="#" onclick="focusSearchBar();">search
30 //! bar</a> at the top of the page.
32 //! Otherwise, you may want to jump to one of these useful sections:
34 //! * [`std::*` modules](#modules)
35 //! * [Primitive types](#primitives)
36 //! * [Standard macros](#macros)
37 //! * [The Rust Prelude](prelude/index.html)
39 //! If this is your first time, the documentation for the standard library is
40 //! written to be casually perused. Clicking on interesting things should
41 //! generally lead you to interesting places. Still, there are important bits
42 //! you don't want to miss, so read on for a tour of the standard library and
43 //! its documentation!
45 //! Once you are familiar with the contents of the standard library you may
46 //! begin to find the verbosity of the prose distracting. At this stage in your
47 //! development you may want to press the `[-]` button near the top of the
48 //! page to collapse it into a more skimmable view.
50 //! While you are looking at that `[-]` button also notice the `[src]`
51 //! button. Rust's API documentation comes with the source code and you are
52 //! encouraged to read it. The standard library source is generally high
53 //! quality and a peek behind the curtains is often enlightening.
55 //! # What is in the standard library documentation?
57 //! First of all, The Rust Standard Library is divided into a number of focused
58 //! modules, [all listed further down this page](#modules). These modules are
59 //! the bedrock upon which all of Rust is forged, and they have mighty names
60 //! like [`std::slice`] and [`std::cmp`]. Modules' documentation typically
61 //! includes an overview of the module along with examples, and are a smart
62 //! place to start familiarizing yourself with the library.
64 //! Second, implicit methods on [primitive types] are documented here. This can
65 //! be a source of confusion for two reasons:
67 //! 1. While primitives are implemented by the compiler, the standard library
68 //! implements methods directly on the primitive types (and it is the only
69 //! library that does so), which are [documented in the section on
70 //! primitives](#primitives).
71 //! 2. The standard library exports many modules *with the same name as
72 //! primitive types*. These define additional items related to the primitive
73 //! type, but not the all-important methods.
75 //! So for example there is a [page for the primitive type
76 //! `i32`](primitive.i32.html) that lists all the methods that can be called on
77 //! 32-bit integers (very useful), and there is a [page for the module
78 //! `std::i32`](i32/index.html) that documents the constant values [`MIN`] and
79 //! [`MAX`](i32/constant.MAX.html) (rarely useful).
81 //! Note the documentation for the primitives [`str`] and [`[T]`][slice] (also
82 //! called 'slice'). Many method calls on [`String`] and [`Vec<T>`] are actually
83 //! calls to methods on [`str`] and [`[T]`][slice] respectively, via [deref
84 //! coercions][deref-coercions].
86 //! Third, the standard library defines [The Rust Prelude], a small collection
87 //! of items - mostly traits - that are imported into every module of every
88 //! crate. The traits in the prelude are pervasive, making the prelude
89 //! documentation a good entry point to learning about the library.
91 //! And finally, the standard library exports a number of standard macros, and
92 //! [lists them on this page](#macros) (technically, not all of the standard
93 //! macros are defined by the standard library - some are defined by the
94 //! compiler - but they are documented here the same). Like the prelude, the
95 //! standard macros are imported by default into all crates.
97 //! # Contributing changes to the documentation
99 //! Check out the rust contribution guidelines [here](
100 //! https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md).
101 //! The source for this documentation can be found on [Github](https://github.com/rust-lang).
102 //! To contribute changes, make sure you read the guidelines first, then submit
103 //! pull-requests for your suggested changes.
105 //! Contributions are appreciated! If you see a part of the docs that can be
106 //! improved, submit a PR, or chat with us first on irc.mozilla.org #rust-docs.
108 //! # A Tour of The Rust Standard Library
110 //! The rest of this crate documentation is dedicated to pointing out notable
111 //! features of The Rust Standard Library.
113 //! ## Containers and collections
115 //! The [`option`] and [`result`] modules define optional and error-handling
116 //! types, [`Option<T>`] and [`Result<T, E>`]. The [`iter`] module defines
117 //! Rust's iterator trait, [`Iterator`], which works with the [`for`] loop to
118 //! access collections.
120 //! The standard library exposes three common ways to deal with contiguous
121 //! regions of memory:
123 //! * [`Vec<T>`] - A heap-allocated *vector* that is resizable at runtime.
124 //! * [`[T; n]`][array] - An inline *array* with a fixed size at compile time.
125 //! * [`[T]`][slice] - A dynamically sized *slice* into any other kind of contiguous
126 //! storage, whether heap-allocated or not.
128 //! Slices can only be handled through some kind of *pointer*, and as such come
129 //! in many flavors such as:
131 //! * `&[T]` - *shared slice*
132 //! * `&mut [T]` - *mutable slice*
133 //! * [`Box<[T]>`][owned slice] - *owned slice*
135 //! [`str`], a UTF-8 string slice, is a primitive type, and the standard library
136 //! defines many methods for it. Rust [`str`]s are typically accessed as
137 //! immutable references: `&str`. Use the owned [`String`] for building and
138 //! mutating strings.
140 //! For converting to strings use the [`format!`] macro, and for converting from
141 //! strings use the [`FromStr`] trait.
143 //! Data may be shared by placing it in a reference-counted box or the [`Rc`]
144 //! type, and if further contained in a [`Cell`] or [`RefCell`], may be mutated
145 //! as well as shared. Likewise, in a concurrent setting it is common to pair an
146 //! atomically-reference-counted box, [`Arc`], with a [`Mutex`] to get the same
149 //! The [`collections`] module defines maps, sets, linked lists and other
150 //! typical collection types, including the common [`HashMap<K, V>`].
152 //! ## Platform abstractions and I/O
154 //! Besides basic data types, the standard library is largely concerned with
155 //! abstracting over differences in common platforms, most notably Windows and
156 //! Unix derivatives.
158 //! Common types of I/O, including [files], [TCP], [UDP], are defined in the
159 //! [`io`], [`fs`], and [`net`] modules.
161 //! The [`thread`] module contains Rust's threading abstractions. [`sync`]
162 //! contains further primitive shared memory types, including [`atomic`] and
163 //! [`mpsc`], which contains the channel types for message passing.
165 //! [I/O]: io/index.html
166 //! [`MIN`]: i32/constant.MIN.html
167 //! [TCP]: net/struct.TcpStream.html
168 //! [The Rust Prelude]: prelude/index.html
169 //! [UDP]: net/struct.UdpSocket.html
170 //! [`::std::env::args`]: env/fn.args.html
171 //! [`Arc`]: sync/struct.Arc.html
172 //! [owned slice]: boxed/index.html
173 //! [`Cell`]: cell/struct.Cell.html
174 //! [`FromStr`]: str/trait.FromStr.html
175 //! [`HashMap<K, V>`]: collections/struct.HashMap.html
176 //! [`Iterator`]: iter/trait.Iterator.html
177 //! [`Mutex`]: sync/struct.Mutex.html
178 //! [`Option<T>`]: option/enum.Option.html
179 //! [`Rc`]: rc/index.html
180 //! [`RefCell`]: cell/struct.RefCell.html
181 //! [`Result<T, E>`]: result/enum.Result.html
182 //! [`String`]: string/struct.String.html
183 //! [`Vec<T>`]: vec/index.html
184 //! [array]: primitive.array.html
185 //! [slice]: primitive.slice.html
186 //! [`atomic`]: sync/atomic/index.html
187 //! [`collections`]: collections/index.html
188 //! [`for`]: ../book/first-edition/loops.html#for
189 //! [`format!`]: macro.format.html
190 //! [`fs`]: fs/index.html
191 //! [`io`]: io/index.html
192 //! [`iter`]: iter/index.html
193 //! [`mpsc`]: sync/mpsc/index.html
194 //! [`net`]: net/index.html
195 //! [`option`]: option/index.html
196 //! [`result`]: result/index.html
197 //! [`std::cmp`]: cmp/index.html
198 //! [`std::slice`]: slice/index.html
199 //! [`str`]: primitive.str.html
200 //! [`sync`]: sync/index.html
201 //! [`thread`]: thread/index.html
202 //! [`use std::env`]: env/index.html
203 //! [`use`]: ../book/first-edition/crates-and-modules.html#importing-modules-with-use
204 //! [crate root]: ../book/first-edition/crates-and-modules.html#basic-terminology-crates-and-modules
205 //! [crates.io]: https://crates.io
206 //! [deref-coercions]: ../book/second-edition/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods
207 //! [files]: fs/struct.File.html
208 //! [multithreading]: thread/index.html
209 //! [other]: #what-is-in-the-standard-library-documentation
210 //! [primitive types]: ../book/first-edition/primitive-types.html
212 #![stable(feature = "rust1", since = "1.0.0")]
213 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
214 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
215 html_root_url = "https://doc.rust-lang.org/nightly/",
216 html_playground_url = "https://play.rust-lang.org/",
217 issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
218 test(no_crate_inject, attr(deny(warnings))),
219 test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))))]
221 // Don't link to std. We are std.
224 #![deny(missing_docs)]
225 #![deny(missing_debug_implementations)]
227 // Tell the compiler to link to either panic_abort or panic_unwind
228 #![needs_panic_runtime]
230 // std may use features in a platform-specific way
231 #![allow(unused_features)]
233 // std is implemented with unstable features, many of which are internal
234 // compiler details that will never be stable
235 #![cfg_attr(test, feature(test, update_panic_count))]
237 #![feature(alloc_error_handler)]
238 #![feature(alloc_system)]
239 #![feature(allocator_api)]
240 #![feature(allocator_internals)]
241 #![feature(allow_internal_unsafe)]
242 #![feature(allow_internal_unstable)]
243 #![feature(align_offset)]
244 #![feature(arbitrary_self_types)]
245 #![feature(array_error_internals)]
247 #![feature(box_syntax)]
248 #![feature(cfg_target_has_atomic)]
249 #![feature(cfg_target_thread_local)]
250 #![feature(cfg_target_vendor)]
251 #![feature(char_error_internals)]
252 #![feature(compiler_builtins_lib)]
253 #![cfg_attr(stage0, feature(const_fn))]
254 #![cfg_attr(not(stage0), feature(min_const_fn))]
255 #![feature(const_int_ops)]
256 #![feature(const_ip)]
257 #![feature(core_intrinsics)]
258 #![feature(dropck_eyepatch)]
259 #![feature(exact_size_is_empty)]
260 #![feature(external_doc)]
261 #![feature(fixed_size_array)]
262 #![feature(fn_traits)]
264 #![feature(futures_api)]
265 #![feature(generator_trait)]
266 #![feature(hashmap_internals)]
267 #![feature(int_error_internals)]
268 #![feature(integer_atomics)]
269 #![feature(lang_items)]
271 #![feature(link_args)]
273 #![cfg_attr(stage0, feature(macro_vis_matcher))]
274 #![feature(needs_panic_runtime)]
275 #![feature(never_type)]
276 #![cfg_attr(not(stage0), feature(nll))]
277 #![feature(exhaustive_patterns)]
278 #![feature(on_unimplemented)]
279 #![feature(optin_builtin_traits)]
280 #![feature(panic_internals)]
281 #![feature(panic_unwind)]
283 #![feature(prelude_import)]
284 #![feature(ptr_internals)]
286 #![feature(rustc_attrs)]
287 #![feature(rustc_const_unstable)]
288 #![feature(std_internals)]
290 #![feature(shrink_to)]
291 #![feature(slice_concat_ext)]
292 #![feature(slice_internals)]
293 #![feature(slice_patterns)]
294 #![feature(staged_api)]
295 #![feature(stmt_expr_attributes)]
296 #![feature(str_internals)]
297 #![feature(rustc_private)]
298 #![feature(thread_local)]
299 #![feature(toowned_clone_into)]
300 #![feature(try_from)]
301 #![feature(try_reserve)]
302 #![feature(unboxed_closures)]
303 #![feature(untagged_unions)]
304 #![feature(unwind_attributes)]
305 #![cfg_attr(stage0, feature(use_extern_macros))]
307 #![feature(doc_masked)]
308 #![feature(doc_spotlight)]
309 #![cfg_attr(all(windows, stage0), feature(used))]
310 #![feature(doc_alias)]
311 #![feature(doc_keyword)]
312 #![feature(panic_info_message)]
313 #![cfg_attr(stage0, feature(panic_implementation))]
314 #![feature(non_exhaustive)]
316 #![default_lib_allocator]
318 // Always use alloc_system during stage0 since we don't know if the alloc_*
319 // crate the stage0 compiler will pick by default is enabled (e.g.
320 // if the user has disabled jemalloc in `./configure`).
321 // `force_alloc_system` is *only* intended as a workaround for local rebuilds
322 // with a rustc without jemalloc.
323 // FIXME(#44236) shouldn't need MSVC logic
324 #[cfg(all(not(target_env = "msvc"),
325 any(all(stage0, not(test)), feature = "force_alloc_system")))]
327 static ALLOC: alloc_system::System = alloc_system::System;
329 // Explicitly import the prelude. The compiler uses this same unstable attribute
330 // to import the prelude implicitly when building crates that depend on std.
335 // Access to Bencher, etc.
336 #[cfg(test)] extern crate test;
337 #[cfg(test)] extern crate rand;
339 // Re-export a few macros from core
340 #[stable(feature = "rust1", since = "1.0.0")]
341 pub use core::{assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne};
342 #[stable(feature = "rust1", since = "1.0.0")]
343 pub use core::{unreachable, unimplemented, write, writeln, try};
345 #[allow(unused_imports)] // macros from `alloc` are not used on all platforms
347 extern crate alloc as alloc_crate;
348 extern crate alloc_system;
352 // We always need an unwinder currently for backtraces
354 #[allow(unused_extern_crates)]
357 // During testing, this crate is not actually the "real" std library, but rather
358 // it links to the real std library, which was compiled from this same source
359 // code. So any lang items std defines are conditionally excluded (or else they
360 // would generate duplicate lang item errors), and any globals it defines are
361 // _not_ the globals used by "real" std. So this import, defined only during
362 // testing gives test-std access to real-std lang items and globals. See #2912
363 #[cfg(test)] extern crate std as realstd;
365 // The standard macros that are not built-in to the compiler.
372 // Public module declarations and re-exports
373 #[stable(feature = "rust1", since = "1.0.0")]
375 #[stable(feature = "rust1", since = "1.0.0")]
377 #[stable(feature = "rust1", since = "1.0.0")]
379 #[stable(feature = "rust1", since = "1.0.0")]
381 #[stable(feature = "rust1", since = "1.0.0")]
382 pub use core::convert;
383 #[stable(feature = "rust1", since = "1.0.0")]
384 pub use core::default;
385 #[stable(feature = "rust1", since = "1.0.0")]
387 #[stable(feature = "rust1", since = "1.0.0")]
388 pub use core::intrinsics;
389 #[stable(feature = "rust1", since = "1.0.0")]
391 #[stable(feature = "rust1", since = "1.0.0")]
392 pub use core::marker;
393 #[stable(feature = "rust1", since = "1.0.0")]
395 #[stable(feature = "rust1", since = "1.0.0")]
397 #[stable(feature = "rust1", since = "1.0.0")]
399 #[stable(feature = "rust1", since = "1.0.0")]
401 #[stable(feature = "rust1", since = "1.0.0")]
402 pub use core::result;
403 #[stable(feature = "rust1", since = "1.0.0")]
404 pub use core::option;
405 #[stable(feature = "rust1", since = "1.0.0")]
407 #[stable(feature = "rust1", since = "1.0.0")]
409 #[stable(feature = "rust1", since = "1.0.0")]
411 #[stable(feature = "rust1", since = "1.0.0")]
413 #[stable(feature = "rust1", since = "1.0.0")]
415 #[stable(feature = "i128", since = "1.26.0")]
417 #[stable(feature = "rust1", since = "1.0.0")]
419 #[stable(feature = "rust1", since = "1.0.0")]
421 #[stable(feature = "rust1", since = "1.0.0")]
423 #[stable(feature = "rust1", since = "1.0.0")]
425 #[stable(feature = "rust1", since = "1.0.0")]
427 #[stable(feature = "rust1", since = "1.0.0")]
428 pub use alloc_crate::boxed;
429 #[stable(feature = "rust1", since = "1.0.0")]
430 pub use alloc_crate::rc;
431 #[stable(feature = "rust1", since = "1.0.0")]
432 pub use alloc_crate::borrow;
433 #[stable(feature = "rust1", since = "1.0.0")]
434 pub use alloc_crate::fmt;
435 #[stable(feature = "rust1", since = "1.0.0")]
436 pub use alloc_crate::format;
437 #[unstable(feature = "pin", issue = "49150")]
439 #[stable(feature = "rust1", since = "1.0.0")]
440 pub use alloc_crate::slice;
441 #[stable(feature = "rust1", since = "1.0.0")]
442 pub use alloc_crate::str;
443 #[stable(feature = "rust1", since = "1.0.0")]
444 pub use alloc_crate::string;
445 #[stable(feature = "rust1", since = "1.0.0")]
446 pub use alloc_crate::vec;
447 #[stable(feature = "rust1", since = "1.0.0")]
449 #[stable(feature = "i128", since = "1.26.0")]
451 #[stable(feature = "core_hint", since = "1.27.0")]
475 #[unstable(feature = "futures_api",
476 reason = "futures in libcore are unstable",
479 //! Types and Traits for working with asynchronous tasks.
481 pub use core::task::*;
483 pub use alloc_crate::task::*;
486 #[unstable(feature = "futures_api",
487 reason = "futures in libcore are unstable",
491 // Platform-abstraction modules
498 // Private support modules
502 // The runtime entry point and a few unstable public functions used by the
506 // Pull in the the `stdsimd` crate directly into libstd. This is the same as
507 // libcore's arch/simd modules where the source of truth here is in a different
508 // repository, but we pull things in here manually to get it into libstd.
510 // Note that the #[cfg] here is intended to do two things. First it allows us to
511 // change the rustc implementation of intrinsics in stage0 by not compiling simd
512 // intrinsics in stage0. Next it doesn't compile anything in test mode as
513 // stdsimd has tons of its own tests which we don't want to run.
514 #[path = "../stdsimd/stdsimd/mod.rs"]
515 #[allow(missing_debug_implementations, missing_docs, dead_code)]
516 #[unstable(feature = "stdsimd", issue = "48556")]
517 #[cfg(all(not(stage0), not(test)))]
520 // A "fake" module needed by the `stdsimd` module to compile, not actually
527 #[stable(feature = "simd_arch", since = "1.27.0")]
528 #[cfg(all(not(stage0), not(test)))]
529 pub use stdsimd::arch;
531 // Include a number of private modules that exist solely to provide
532 // the rustdoc documentation for primitive types. Using `include!`
533 // because rustdoc only looks for these modules at the crate level.
534 include!("primitive_docs.rs");
536 // Include a number of private modules that exist solely to provide
537 // the rustdoc documentation for the existing keywords. Using `include!`
538 // because rustdoc only looks for these modules at the crate level.
539 include!("keyword_docs.rs");