]> git.lizzy.rs Git - rust.git/blob - src/libstd/lib.rs
Rollup merge of #58440 - gnzlbg:v6, r=japaric
[rust.git] / src / libstd / lib.rs
1 //! # The Rust Standard Library
2 //!
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].
9 //!
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`].
13 //!
14 //! # How to read this documentation
15 //!
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.
19 //!
20 //! Otherwise, you may want to jump to one of these useful sections:
21 //!
22 //! * [`std::*` modules](#modules)
23 //! * [Primitive types](#primitives)
24 //! * [Standard macros](#macros)
25 //! * [The Rust Prelude](prelude/index.html)
26 //!
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!
32 //!
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.
37 //!
38 //! While you are looking at that `[-]` button also notice the `[src]`
39 //! button. 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.
42 //!
43 //! # What is in the standard library documentation?
44 //!
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.
51 //!
52 //! Second, implicit methods on [primitive types] are documented here. This can
53 //! be a source of confusion for two reasons:
54 //!
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.
62 //!
63 //! So for example there is a [page for the primitive type
64 //! `i32`](primitive.i32.html) 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`](i32/index.html) that documents the constant values [`MIN`] and
67 //! [`MAX`](i32/constant.MAX.html) (rarely useful).
68 //!
69 //! Note the documentation for the primitives [`str`] and [`[T]`][slice] (also
70 //! called 'slice'). Many method calls on [`String`] and [`Vec<T>`] are actually
71 //! calls to methods on [`str`] and [`[T]`][slice] respectively, via [deref
72 //! coercions][deref-coercions].
73 //!
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.
78 //!
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.
84 //!
85 //! # Contributing changes to the documentation
86 //!
87 //! Check out the rust contribution guidelines [here](
88 //! https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md).
89 //! The source for this documentation can be found on [Github](https://github.com/rust-lang).
90 //! To contribute changes, make sure you read the guidelines first, then submit
91 //! pull-requests for your suggested changes.
92 //!
93 //! Contributions are appreciated! If you see a part of the docs that can be
94 //! improved, submit a PR, or chat with us first on irc.mozilla.org #rust-docs.
95 //!
96 //! # A Tour of The Rust Standard Library
97 //!
98 //! The rest of this crate documentation is dedicated to pointing out notable
99 //! features of The Rust Standard Library.
100 //!
101 //! ## Containers and collections
102 //!
103 //! The [`option`] and [`result`] modules define optional and error-handling
104 //! types, [`Option<T>`] and [`Result<T, E>`]. The [`iter`] module defines
105 //! Rust's iterator trait, [`Iterator`], which works with the [`for`] loop to
106 //! access collections.
107 //!
108 //! The standard library exposes three common ways to deal with contiguous
109 //! regions of memory:
110 //!
111 //! * [`Vec<T>`] - A heap-allocated *vector* that is resizable at runtime.
112 //! * [`[T; n]`][array] - An inline *array* with a fixed size at compile time.
113 //! * [`[T]`][slice] - A dynamically sized *slice* into any other kind of contiguous
114 //!   storage, whether heap-allocated or not.
115 //!
116 //! Slices can only be handled through some kind of *pointer*, and as such come
117 //! in many flavors such as:
118 //!
119 //! * `&[T]` - *shared slice*
120 //! * `&mut [T]` - *mutable slice*
121 //! * [`Box<[T]>`][owned slice] - *owned slice*
122 //!
123 //! [`str`], a UTF-8 string slice, is a primitive type, and the standard library
124 //! defines many methods for it. Rust [`str`]s are typically accessed as
125 //! immutable references: `&str`. Use the owned [`String`] for building and
126 //! mutating strings.
127 //!
128 //! For converting to strings use the [`format!`] macro, and for converting from
129 //! strings use the [`FromStr`] trait.
130 //!
131 //! Data may be shared by placing it in a reference-counted box or the [`Rc`]
132 //! type, and if further contained in a [`Cell`] or [`RefCell`], may be mutated
133 //! as well as shared. Likewise, in a concurrent setting it is common to pair an
134 //! atomically-reference-counted box, [`Arc`], with a [`Mutex`] to get the same
135 //! effect.
136 //!
137 //! The [`collections`] module defines maps, sets, linked lists and other
138 //! typical collection types, including the common [`HashMap<K, V>`].
139 //!
140 //! ## Platform abstractions and I/O
141 //!
142 //! Besides basic data types, the standard library is largely concerned with
143 //! abstracting over differences in common platforms, most notably Windows and
144 //! Unix derivatives.
145 //!
146 //! Common types of I/O, including [files], [TCP], [UDP], are defined in the
147 //! [`io`], [`fs`], and [`net`] modules.
148 //!
149 //! The [`thread`] module contains Rust's threading abstractions. [`sync`]
150 //! contains further primitive shared memory types, including [`atomic`] and
151 //! [`mpsc`], which contains the channel types for message passing.
152 //!
153 //! [I/O]: io/index.html
154 //! [`MIN`]: i32/constant.MIN.html
155 //! [TCP]: net/struct.TcpStream.html
156 //! [The Rust Prelude]: prelude/index.html
157 //! [UDP]: net/struct.UdpSocket.html
158 //! [`Arc`]: sync/struct.Arc.html
159 //! [owned slice]: boxed/index.html
160 //! [`Cell`]: cell/struct.Cell.html
161 //! [`FromStr`]: str/trait.FromStr.html
162 //! [`HashMap<K, V>`]: collections/struct.HashMap.html
163 //! [`Iterator`]: iter/trait.Iterator.html
164 //! [`Mutex`]: sync/struct.Mutex.html
165 //! [`Option<T>`]: option/enum.Option.html
166 //! [`Rc`]: rc/index.html
167 //! [`RefCell`]: cell/struct.RefCell.html
168 //! [`Result<T, E>`]: result/enum.Result.html
169 //! [`String`]: string/struct.String.html
170 //! [`Vec<T>`]: vec/index.html
171 //! [array]: primitive.array.html
172 //! [slice]: primitive.slice.html
173 //! [`atomic`]: sync/atomic/index.html
174 //! [`collections`]: collections/index.html
175 //! [`for`]: ../book/ch03-05-control-flow.html#looping-through-a-collection-with-for
176 //! [`format!`]: macro.format.html
177 //! [`fs`]: fs/index.html
178 //! [`io`]: io/index.html
179 //! [`iter`]: iter/index.html
180 //! [`mpsc`]: sync/mpsc/index.html
181 //! [`net`]: net/index.html
182 //! [`option`]: option/index.html
183 //! [`result`]: result/index.html
184 //! [`std::cmp`]: cmp/index.html
185 //! [`std::slice`]: slice/index.html
186 //! [`str`]: primitive.str.html
187 //! [`sync`]: sync/index.html
188 //! [`thread`]: thread/index.html
189 //! [`use std::env`]: env/index.html
190 //! [`use`]: ../book/ch07-02-modules-and-use-to-control-scope-and-privacy.html#the-use-keyword-to-bring-paths-into-a-scope
191 //! [crates.io]: https://crates.io
192 //! [deref-coercions]: ../book/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods
193 //! [files]: fs/struct.File.html
194 //! [multithreading]: thread/index.html
195 //! [other]: #what-is-in-the-standard-library-documentation
196 //! [primitive types]: ../book/ch03-02-data-types.html
197
198 #![stable(feature = "rust1", since = "1.0.0")]
199 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
200        html_playground_url = "https://play.rust-lang.org/",
201        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
202        test(no_crate_inject, attr(deny(warnings))),
203        test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))))]
204
205 // Don't link to std. We are std.
206 #![no_std]
207
208 #![deny(missing_docs)]
209 #![deny(intra_doc_link_resolution_failure)]
210 #![deny(missing_debug_implementations)]
211
212 // Tell the compiler to link to either panic_abort or panic_unwind
213 #![needs_panic_runtime]
214
215 // std may use features in a platform-specific way
216 #![allow(unused_features)]
217
218 // std is implemented with unstable features, many of which are internal
219 // compiler details that will never be stable
220 #![cfg_attr(test, feature(test, update_panic_count))]
221 #![feature(alloc)]
222 #![feature(alloc_error_handler)]
223 #![feature(allocator_api)]
224 #![feature(allocator_internals)]
225 #![feature(allow_internal_unsafe)]
226 #![feature(allow_internal_unstable)]
227 #![feature(align_offset)]
228 #![feature(arbitrary_self_types)]
229 #![feature(array_error_internals)]
230 #![feature(asm)]
231 #![feature(box_syntax)]
232 #![feature(c_variadic)]
233 #![feature(cfg_target_has_atomic)]
234 #![feature(cfg_target_thread_local)]
235 #![feature(char_error_internals)]
236 #![feature(compiler_builtins_lib)]
237 #![feature(concat_idents)]
238 #![feature(const_raw_ptr_deref)]
239 #![feature(const_cstr_unchecked)]
240 #![feature(core_intrinsics)]
241 #![feature(dropck_eyepatch)]
242 #![feature(duration_constants)]
243 #![feature(exact_size_is_empty)]
244 #![feature(external_doc)]
245 #![feature(fixed_size_array)]
246 #![feature(fn_traits)]
247 #![feature(fnbox)]
248 #![feature(futures_api)]
249 #![feature(generator_trait)]
250 #![feature(hashmap_internals)]
251 #![feature(int_error_internals)]
252 #![feature(integer_atomics)]
253 #![feature(lang_items)]
254 #![feature(libc)]
255 #![feature(link_args)]
256 #![feature(linkage)]
257 #![feature(needs_panic_runtime)]
258 #![feature(never_type)]
259 #![feature(nll)]
260 #![feature(exhaustive_patterns)]
261 #![feature(on_unimplemented)]
262 #![feature(optin_builtin_traits)]
263 #![feature(panic_internals)]
264 #![feature(panic_unwind)]
265 #![feature(prelude_import)]
266 #![feature(ptr_internals)]
267 #![feature(raw)]
268 #![feature(hash_raw_entry)]
269 #![feature(rustc_attrs)]
270 #![feature(rustc_const_unstable)]
271 #![feature(std_internals)]
272 #![feature(stdsimd)]
273 #![feature(shrink_to)]
274 #![feature(slice_concat_ext)]
275 #![feature(slice_internals)]
276 #![feature(slice_patterns)]
277 #![feature(staged_api)]
278 #![feature(stmt_expr_attributes)]
279 #![feature(str_internals)]
280 #![feature(renamed_spin_loop)]
281 #![feature(rustc_private)]
282 #![feature(thread_local)]
283 #![feature(toowned_clone_into)]
284 #![feature(try_from)]
285 #![feature(try_reserve)]
286 #![feature(unboxed_closures)]
287 #![feature(untagged_unions)]
288 #![feature(unwind_attributes)]
289 #![feature(doc_cfg)]
290 #![feature(doc_masked)]
291 #![feature(doc_spotlight)]
292 #![feature(doc_alias)]
293 #![feature(doc_keyword)]
294 #![feature(panic_info_message)]
295 #![feature(non_exhaustive)]
296 #![feature(alloc_layout_extra)]
297 #![feature(maybe_uninit)]
298 #![cfg_attr(all(target_vendor = "fortanix", target_env = "sgx"),
299             feature(global_asm, range_contains, slice_index_methods,
300                     decl_macro, coerce_unsized, sgx_platform, ptr_wrapping_offset_from))]
301
302 #![default_lib_allocator]
303
304 // Explicitly import the prelude. The compiler uses this same unstable attribute
305 // to import the prelude implicitly when building crates that depend on std.
306 #[prelude_import]
307 #[allow(unused)]
308 use prelude::v1::*;
309
310 // Access to Bencher, etc.
311 #[cfg(test)] extern crate test;
312 #[cfg(test)] extern crate rand;
313
314 // Re-export a few macros from core
315 #[stable(feature = "rust1", since = "1.0.0")]
316 pub use core::{assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne};
317 #[stable(feature = "rust1", since = "1.0.0")]
318 pub use core::{unreachable, unimplemented, write, writeln, try};
319
320 #[allow(unused_imports)] // macros from `alloc` are not used on all platforms
321 #[macro_use]
322 extern crate alloc as alloc_crate;
323 #[doc(masked)]
324 extern crate libc;
325 extern crate rustc_demangle;
326
327 // We always need an unwinder currently for backtraces
328 #[doc(masked)]
329 #[allow(unused_extern_crates)]
330 extern crate unwind;
331
332 #[cfg(feature = "backtrace")]
333 extern crate backtrace_sys;
334
335 // During testing, this crate is not actually the "real" std library, but rather
336 // it links to the real std library, which was compiled from this same source
337 // code. So any lang items std defines are conditionally excluded (or else they
338 // would generate duplicate lang item errors), and any globals it defines are
339 // _not_ the globals used by "real" std. So this import, defined only during
340 // testing gives test-std access to real-std lang items and globals. See #2912
341 #[cfg(test)] extern crate std as realstd;
342
343 #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
344 extern crate fortanix_sgx_abi;
345
346 // The standard macros that are not built-in to the compiler.
347 #[macro_use]
348 mod macros;
349
350 // The Rust prelude
351 pub mod prelude;
352
353 // Public module declarations and re-exports
354 #[stable(feature = "rust1", since = "1.0.0")]
355 pub use core::any;
356 #[stable(feature = "simd_arch", since = "1.27.0")]
357 #[doc(no_inline)]
358 pub use core::arch;
359 #[stable(feature = "rust1", since = "1.0.0")]
360 pub use core::cell;
361 #[stable(feature = "rust1", since = "1.0.0")]
362 pub use core::clone;
363 #[stable(feature = "rust1", since = "1.0.0")]
364 pub use core::cmp;
365 #[stable(feature = "rust1", since = "1.0.0")]
366 pub use core::convert;
367 #[stable(feature = "rust1", since = "1.0.0")]
368 pub use core::default;
369 #[stable(feature = "rust1", since = "1.0.0")]
370 pub use core::hash;
371 #[stable(feature = "rust1", since = "1.0.0")]
372 pub use core::intrinsics;
373 #[stable(feature = "rust1", since = "1.0.0")]
374 pub use core::iter;
375 #[stable(feature = "rust1", since = "1.0.0")]
376 pub use core::marker;
377 #[stable(feature = "rust1", since = "1.0.0")]
378 pub use core::mem;
379 #[stable(feature = "rust1", since = "1.0.0")]
380 pub use core::ops;
381 #[stable(feature = "rust1", since = "1.0.0")]
382 pub use core::ptr;
383 #[stable(feature = "rust1", since = "1.0.0")]
384 pub use core::raw;
385 #[stable(feature = "rust1", since = "1.0.0")]
386 pub use core::result;
387 #[stable(feature = "rust1", since = "1.0.0")]
388 pub use core::option;
389 #[stable(feature = "rust1", since = "1.0.0")]
390 pub use core::isize;
391 #[stable(feature = "rust1", since = "1.0.0")]
392 pub use core::i8;
393 #[stable(feature = "rust1", since = "1.0.0")]
394 pub use core::i16;
395 #[stable(feature = "rust1", since = "1.0.0")]
396 pub use core::i32;
397 #[stable(feature = "rust1", since = "1.0.0")]
398 pub use core::i64;
399 #[stable(feature = "i128", since = "1.26.0")]
400 pub use core::i128;
401 #[stable(feature = "rust1", since = "1.0.0")]
402 pub use core::usize;
403 #[stable(feature = "rust1", since = "1.0.0")]
404 pub use core::u8;
405 #[stable(feature = "rust1", since = "1.0.0")]
406 pub use core::u16;
407 #[stable(feature = "rust1", since = "1.0.0")]
408 pub use core::u32;
409 #[stable(feature = "rust1", since = "1.0.0")]
410 pub use core::u64;
411 #[stable(feature = "rust1", since = "1.0.0")]
412 pub use alloc_crate::boxed;
413 #[stable(feature = "rust1", since = "1.0.0")]
414 pub use alloc_crate::rc;
415 #[stable(feature = "rust1", since = "1.0.0")]
416 pub use alloc_crate::borrow;
417 #[stable(feature = "rust1", since = "1.0.0")]
418 pub use alloc_crate::fmt;
419 #[stable(feature = "rust1", since = "1.0.0")]
420 pub use alloc_crate::format;
421 #[stable(feature = "pin", since = "1.33.0")]
422 pub use core::pin;
423 #[stable(feature = "rust1", since = "1.0.0")]
424 pub use alloc_crate::slice;
425 #[stable(feature = "rust1", since = "1.0.0")]
426 pub use alloc_crate::str;
427 #[stable(feature = "rust1", since = "1.0.0")]
428 pub use alloc_crate::string;
429 #[stable(feature = "rust1", since = "1.0.0")]
430 pub use alloc_crate::vec;
431 #[stable(feature = "rust1", since = "1.0.0")]
432 pub use core::char;
433 #[stable(feature = "i128", since = "1.26.0")]
434 pub use core::u128;
435 #[stable(feature = "core_hint", since = "1.27.0")]
436 pub use core::hint;
437
438 pub mod f32;
439 pub mod f64;
440
441 #[macro_use]
442 pub mod thread;
443 pub mod ascii;
444 pub mod collections;
445 pub mod env;
446 pub mod error;
447 pub mod ffi;
448 pub mod fs;
449 pub mod io;
450 pub mod net;
451 pub mod num;
452 pub mod os;
453 pub mod panic;
454 pub mod path;
455 pub mod process;
456 pub mod sync;
457 pub mod time;
458
459 #[unstable(feature = "futures_api",
460            reason = "futures in libcore are unstable",
461            issue = "50547")]
462 pub mod task {
463     //! Types and Traits for working with asynchronous tasks.
464     #[doc(inline)]
465     pub use core::task::*;
466 }
467
468 #[unstable(feature = "futures_api",
469            reason = "futures in libcore are unstable",
470            issue = "50547")]
471 pub mod future;
472
473 // Platform-abstraction modules
474 #[macro_use]
475 mod sys_common;
476 mod sys;
477
478 pub mod alloc;
479
480 // Private support modules
481 mod panicking;
482 mod memchr;
483
484 // The runtime entry point and a few unstable public functions used by the
485 // compiler
486 pub mod rt;
487
488 // Pull in the `std_detect` crate directly into libstd. The contents of
489 // `std_detect` are in a different repository: rust-lang-nursery/stdsimd.
490 //
491 // `std_detect` depends on libstd, but the contents of this module are
492 // set up in such a way that directly pulling it here works such that the
493 // crate uses the this crate as its libstd.
494 #[path = "../stdsimd/crates/std_detect/src/mod.rs"]
495 #[allow(missing_debug_implementations, missing_docs, dead_code)]
496 #[unstable(feature = "stdsimd", issue = "48556")]
497 #[cfg(not(test))]
498 mod std_detect;
499
500 #[doc(hidden)]
501 #[unstable(feature = "stdsimd", issue = "48556")]
502 #[cfg(not(test))]
503 pub use std_detect::detect;
504
505 // Include a number of private modules that exist solely to provide
506 // the rustdoc documentation for primitive types. Using `include!`
507 // because rustdoc only looks for these modules at the crate level.
508 include!("primitive_docs.rs");
509
510 // Include a number of private modules that exist solely to provide
511 // the rustdoc documentation for the existing keywords. Using `include!`
512 // because rustdoc only looks for these modules at the crate level.
513 include!("keyword_docs.rs");