]> git.proxmox.com Git - rustc.git/blob - src/libstd/lib.rs
New upstream version 1.41.1+dfsg1
[rustc.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-defining-modules-to-control-scope-and-privacy.html
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 #![warn(deprecated_in_future)]
209 #![warn(missing_docs)]
210 #![warn(missing_debug_implementations)]
211 #![deny(intra_doc_link_resolution_failure)] // rustdoc is run without -D warnings
212 #![allow(explicit_outlives_requirements)]
213 #![allow(unused_lifetimes)]
214
215 // Tell the compiler to link to either panic_abort or panic_unwind
216 #![needs_panic_runtime]
217
218 // std may use features in a platform-specific way
219 #![allow(unused_features)]
220
221 #![cfg_attr(test, feature(print_internals, set_stdio, update_panic_count))]
222 #![cfg_attr(all(target_vendor = "fortanix", target_env = "sgx"),
223 feature(slice_index_methods, coerce_unsized,
224 sgx_platform, ptr_wrapping_offset_from))]
225 #![cfg_attr(all(test, target_vendor = "fortanix", target_env = "sgx"),
226 feature(fixed_size_array, maybe_uninit_extra))]
227
228 // std is implemented with unstable features, many of which are internal
229 // compiler details that will never be stable
230 // NB: the following list is sorted to minimize merge conflicts.
231 #![feature(alloc_error_handler)]
232 #![feature(alloc_layout_extra)]
233 #![feature(allocator_api)]
234 #![feature(allocator_internals)]
235 #![feature(allow_internal_unsafe)]
236 #![feature(allow_internal_unstable)]
237 #![feature(atomic_mut_ptr)]
238 #![feature(arbitrary_self_types)]
239 #![feature(array_error_internals)]
240 #![feature(asm)]
241 #![feature(associated_type_bounds)]
242 #![feature(box_syntax)]
243 #![feature(c_variadic)]
244 #![feature(cfg_target_has_atomic)]
245 #![feature(cfg_target_thread_local)]
246 #![feature(char_error_internals)]
247 #![feature(clamp)]
248 #![feature(concat_idents)]
249 #![feature(const_cstr_unchecked)]
250 #![feature(const_raw_ptr_deref)]
251 #![feature(container_error_extra)]
252 #![feature(core_intrinsics)]
253 #![feature(custom_test_frameworks)]
254 #![feature(decl_macro)]
255 #![feature(doc_alias)]
256 #![feature(doc_cfg)]
257 #![feature(doc_keyword)]
258 #![feature(doc_masked)]
259 #![feature(doc_spotlight)]
260 #![feature(dropck_eyepatch)]
261 #![feature(duration_constants)]
262 #![feature(exact_size_is_empty)]
263 #![feature(exhaustive_patterns)]
264 #![feature(external_doc)]
265 #![feature(fn_traits)]
266 #![feature(format_args_nl)]
267 #![feature(generator_trait)]
268 #![feature(global_asm)]
269 #![feature(hash_raw_entry)]
270 #![feature(hashmap_internals)]
271 #![feature(int_error_internals)]
272 #![feature(int_error_matching)]
273 #![feature(integer_atomics)]
274 #![feature(lang_items)]
275 #![feature(libc)]
276 #![feature(link_args)]
277 #![feature(linkage)]
278 #![feature(log_syntax)]
279 #![feature(manually_drop_take)]
280 #![feature(matches_macro)]
281 #![feature(maybe_uninit_ref)]
282 #![feature(maybe_uninit_slice)]
283 #![feature(needs_panic_runtime)]
284 #![feature(never_type)]
285 #![feature(nll)]
286 #![cfg_attr(bootstrap, feature(on_unimplemented))]
287 #![feature(optin_builtin_traits)]
288 #![feature(panic_info_message)]
289 #![feature(panic_internals)]
290 #![feature(panic_unwind)]
291 #![feature(prelude_import)]
292 #![feature(ptr_internals)]
293 #![feature(raw)]
294 #![feature(renamed_spin_loop)]
295 #![feature(rustc_attrs)]
296 #![cfg_attr(bootstrap, feature(rustc_const_unstable))]
297 #![feature(rustc_private)]
298 #![feature(shrink_to)]
299 #![feature(slice_concat_ext)]
300 #![feature(slice_internals)]
301 #![feature(slice_patterns)]
302 #![feature(specialization)]
303 #![feature(staged_api)]
304 #![feature(std_internals)]
305 #![feature(stdsimd)]
306 #![feature(stmt_expr_attributes)]
307 #![feature(str_internals)]
308 #![feature(test)]
309 #![feature(thread_local)]
310 #![feature(toowned_clone_into)]
311 #![feature(trace_macros)]
312 #![feature(try_reserve)]
313 #![feature(unboxed_closures)]
314 #![feature(untagged_unions)]
315 #![feature(unwind_attributes)]
316 // NB: the above list is sorted to minimize merge conflicts.
317
318 #![default_lib_allocator]
319
320 // Explicitly import the prelude. The compiler uses this same unstable attribute
321 // to import the prelude implicitly when building crates that depend on std.
322 #[prelude_import]
323 #[allow(unused)]
324 use prelude::v1::*;
325
326 // Access to Bencher, etc.
327 #[cfg(test)] extern crate test;
328
329 #[allow(unused_imports)] // macros from `alloc` are not used on all platforms
330 #[macro_use]
331 extern crate alloc as alloc_crate;
332 #[doc(masked)]
333 #[allow(unused_extern_crates)]
334 extern crate libc;
335
336 // We always need an unwinder currently for backtraces
337 #[doc(masked)]
338 #[allow(unused_extern_crates)]
339 extern crate unwind;
340
341 // Only needed for now for the `std_detect` module until that crate changes to
342 // use `cfg_if::cfg_if!`
343 #[macro_use]
344 #[cfg(not(test))]
345 extern crate cfg_if;
346
347 // During testing, this crate is not actually the "real" std library, but rather
348 // it links to the real std library, which was compiled from this same source
349 // code. So any lang items std defines are conditionally excluded (or else they
350 // would generate duplicate lang item errors), and any globals it defines are
351 // _not_ the globals used by "real" std. So this import, defined only during
352 // testing gives test-std access to real-std lang items and globals. See #2912
353 #[cfg(test)] extern crate std as realstd;
354
355 // The standard macros that are not built-in to the compiler.
356 #[macro_use]
357 mod macros;
358
359 // The Rust prelude
360 pub mod prelude;
361
362 // Public module declarations and re-exports
363 #[stable(feature = "rust1", since = "1.0.0")]
364 pub use core::any;
365 #[stable(feature = "simd_arch", since = "1.27.0")]
366 #[doc(no_inline)]
367 pub use core::arch;
368 #[stable(feature = "rust1", since = "1.0.0")]
369 pub use core::cell;
370 #[stable(feature = "rust1", since = "1.0.0")]
371 pub use core::clone;
372 #[stable(feature = "rust1", since = "1.0.0")]
373 pub use core::cmp;
374 #[stable(feature = "rust1", since = "1.0.0")]
375 pub use core::convert;
376 #[stable(feature = "rust1", since = "1.0.0")]
377 pub use core::default;
378 #[stable(feature = "rust1", since = "1.0.0")]
379 pub use core::hash;
380 #[stable(feature = "rust1", since = "1.0.0")]
381 pub use core::intrinsics;
382 #[stable(feature = "rust1", since = "1.0.0")]
383 pub use core::iter;
384 #[stable(feature = "rust1", since = "1.0.0")]
385 pub use core::marker;
386 #[stable(feature = "rust1", since = "1.0.0")]
387 pub use core::mem;
388 #[stable(feature = "rust1", since = "1.0.0")]
389 pub use core::ops;
390 #[stable(feature = "rust1", since = "1.0.0")]
391 pub use core::ptr;
392 #[stable(feature = "rust1", since = "1.0.0")]
393 pub use core::raw;
394 #[stable(feature = "rust1", since = "1.0.0")]
395 pub use core::result;
396 #[stable(feature = "rust1", since = "1.0.0")]
397 pub use core::option;
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub use core::isize;
400 #[stable(feature = "rust1", since = "1.0.0")]
401 pub use core::i8;
402 #[stable(feature = "rust1", since = "1.0.0")]
403 pub use core::i16;
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub use core::i32;
406 #[stable(feature = "rust1", since = "1.0.0")]
407 pub use core::i64;
408 #[stable(feature = "i128", since = "1.26.0")]
409 pub use core::i128;
410 #[stable(feature = "rust1", since = "1.0.0")]
411 pub use core::usize;
412 #[stable(feature = "rust1", since = "1.0.0")]
413 pub use core::u8;
414 #[stable(feature = "rust1", since = "1.0.0")]
415 pub use core::u16;
416 #[stable(feature = "rust1", since = "1.0.0")]
417 pub use core::u32;
418 #[stable(feature = "rust1", since = "1.0.0")]
419 pub use core::u64;
420 #[stable(feature = "rust1", since = "1.0.0")]
421 pub use alloc_crate::boxed;
422 #[stable(feature = "rust1", since = "1.0.0")]
423 pub use alloc_crate::rc;
424 #[stable(feature = "rust1", since = "1.0.0")]
425 pub use alloc_crate::borrow;
426 #[stable(feature = "rust1", since = "1.0.0")]
427 pub use alloc_crate::fmt;
428 #[stable(feature = "rust1", since = "1.0.0")]
429 pub use alloc_crate::format;
430 #[stable(feature = "pin", since = "1.33.0")]
431 pub use core::pin;
432 #[stable(feature = "rust1", since = "1.0.0")]
433 pub use alloc_crate::slice;
434 #[stable(feature = "rust1", since = "1.0.0")]
435 pub use alloc_crate::str;
436 #[stable(feature = "rust1", since = "1.0.0")]
437 pub use alloc_crate::string;
438 #[stable(feature = "rust1", since = "1.0.0")]
439 pub use alloc_crate::vec;
440 #[stable(feature = "rust1", since = "1.0.0")]
441 pub use core::char;
442 #[stable(feature = "i128", since = "1.26.0")]
443 pub use core::u128;
444 #[stable(feature = "core_hint", since = "1.27.0")]
445 pub use core::hint;
446 #[stable(feature = "core_array", since = "1.36.0")]
447 pub use core::array;
448
449 pub mod f32;
450 pub mod f64;
451
452 #[macro_use]
453 pub mod thread;
454 pub mod ascii;
455 pub mod backtrace;
456 pub mod collections;
457 pub mod env;
458 pub mod error;
459 pub mod ffi;
460 pub mod fs;
461 pub mod io;
462 pub mod net;
463 pub mod num;
464 pub mod os;
465 pub mod panic;
466 pub mod path;
467 pub mod process;
468 pub mod sync;
469 pub mod time;
470
471 #[stable(feature = "futures_api", since = "1.36.0")]
472 pub mod task {
473 //! Types and Traits for working with asynchronous tasks.
474 #[doc(inline)]
475 #[stable(feature = "futures_api", since = "1.36.0")]
476 pub use core::task::*;
477 }
478
479 #[stable(feature = "futures_api", since = "1.36.0")]
480 pub mod future;
481
482 // Platform-abstraction modules
483 #[macro_use]
484 mod sys_common;
485 mod sys;
486
487 pub mod alloc;
488
489 // Private support modules
490 mod panicking;
491 mod memchr;
492
493 // The runtime entry point and a few unstable public functions used by the
494 // compiler
495 pub mod rt;
496
497 // Pull in the `std_detect` crate directly into libstd. The contents of
498 // `std_detect` are in a different repository: rust-lang/stdarch.
499 //
500 // `std_detect` depends on libstd, but the contents of this module are
501 // set up in such a way that directly pulling it here works such that the
502 // crate uses the this crate as its libstd.
503 #[path = "../stdarch/crates/std_detect/src/mod.rs"]
504 #[allow(missing_debug_implementations, missing_docs, dead_code)]
505 #[unstable(feature = "stdsimd", issue = "48556")]
506 #[cfg(not(test))]
507 mod std_detect;
508
509 #[doc(hidden)]
510 #[unstable(feature = "stdsimd", issue = "48556")]
511 #[cfg(not(test))]
512 pub use std_detect::detect;
513
514 // Re-export macros defined in libcore.
515 #[stable(feature = "rust1", since = "1.0.0")]
516 #[allow(deprecated, deprecated_in_future)]
517 pub use core::{
518 // Stable
519 assert_eq,
520 assert_ne,
521 debug_assert_eq,
522 debug_assert_ne,
523 debug_assert,
524 r#try,
525 unimplemented,
526 unreachable,
527 write,
528 writeln,
529 // Unstable
530 todo,
531 matches,
532 };
533
534 // Re-export built-in macros defined through libcore.
535 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
536 pub use core::{
537 // Stable
538 assert,
539 cfg,
540 column,
541 compile_error,
542 concat,
543 env,
544 file,
545 format_args,
546 include,
547 include_bytes,
548 include_str,
549 line,
550 module_path,
551 option_env,
552 stringify,
553 // Unstable
554 asm,
555 concat_idents,
556 format_args_nl,
557 global_asm,
558 log_syntax,
559 trace_macros,
560 };
561
562 // Include a number of private modules that exist solely to provide
563 // the rustdoc documentation for primitive types. Using `include!`
564 // because rustdoc only looks for these modules at the crate level.
565 include!("primitive_docs.rs");
566
567 // Include a number of private modules that exist solely to provide
568 // the rustdoc documentation for the existing keywords. Using `include!`
569 // because rustdoc only looks for these modules at the crate level.
570 include!("keyword_docs.rs");