]> git.proxmox.com Git - rustc.git/blob - library/std/src/lib.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / 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]
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`] that documents the constant values [`MIN`] and [`MAX`] (rarely
67 //! 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://rustc-dev-guide.rust-lang.org/getting-started.html).
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.
93 //!
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]
96 //! #docs.
97 //!
98 //! # A Tour of The Rust Standard Library
99 //!
100 //! The rest of this crate documentation is dedicated to pointing out notable
101 //! features of The Rust Standard Library.
102 //!
103 //! ## Containers and collections
104 //!
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.
109 //!
110 //! The standard library exposes three common ways to deal with contiguous
111 //! regions of memory:
112 //!
113 //! * [`Vec<T>`] - A heap-allocated *vector* that is resizable at runtime.
114 //! * [`[T; n]`][array] - An inline *array* with a fixed size at compile time.
115 //! * [`[T]`][slice] - A dynamically sized *slice* into any other kind of contiguous
116 //! storage, whether heap-allocated or not.
117 //!
118 //! Slices can only be handled through some kind of *pointer*, and as such come
119 //! in many flavors such as:
120 //!
121 //! * `&[T]` - *shared slice*
122 //! * `&mut [T]` - *mutable slice*
123 //! * [`Box<[T]>`][owned slice] - *owned slice*
124 //!
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.
129 //!
130 //! For converting to strings use the [`format!`] macro, and for converting from
131 //! strings use the [`FromStr`] trait.
132 //!
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
137 //! effect.
138 //!
139 //! The [`collections`] module defines maps, sets, linked lists and other
140 //! typical collection types, including the common [`HashMap<K, V>`].
141 //!
142 //! ## Platform abstractions and I/O
143 //!
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.
147 //!
148 //! Common types of I/O, including [files], [TCP], [UDP], are defined in the
149 //! [`io`], [`fs`], and [`net`] modules.
150 //!
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.
154 //!
155 //! [I/O]: io
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
169 //! [`Rc`]: rc::Rc
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`]: 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
189 #![cfg_attr(not(feature = "restricted-std"), stable(feature = "rust1", since = "1.0.0"))]
190 #![cfg_attr(feature = "restricted-std", unstable(feature = "restricted_std", issue = "none"))]
191 #![doc(
192 html_root_url = "https://doc.rust-lang.org/nightly/",
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)))
197 )]
198 // Don't link to std. We are std.
199 #![no_std]
200 #![warn(deprecated_in_future)]
201 #![warn(missing_docs)]
202 #![warn(missing_debug_implementations)]
203 #![allow(explicit_outlives_requirements)]
204 #![allow(unused_lifetimes)]
205 // Tell the compiler to link to either panic_abort or panic_unwind
206 #![needs_panic_runtime]
207 // std may use features in a platform-specific way
208 #![allow(unused_features)]
209 #![cfg_attr(test, feature(print_internals, set_stdio, update_panic_count))]
210 #![cfg_attr(
211 all(target_vendor = "fortanix", target_env = "sgx"),
212 feature(slice_index_methods, coerce_unsized, sgx_platform)
213 )]
214 #![cfg_attr(all(test, target_vendor = "fortanix", target_env = "sgx"), feature(fixed_size_array))]
215 // std is implemented with unstable features, many of which are internal
216 // compiler details that will never be stable
217 // NB: the following list is sorted to minimize merge conflicts.
218 #![feature(alloc_error_handler)]
219 #![feature(alloc_layout_extra)]
220 #![feature(allocator_api)]
221 #![feature(allocator_internals)]
222 #![feature(allow_internal_unsafe)]
223 #![feature(allow_internal_unstable)]
224 #![feature(arbitrary_self_types)]
225 #![feature(array_error_internals)]
226 #![feature(asm)]
227 #![feature(associated_type_bounds)]
228 #![feature(atomic_mut_ptr)]
229 #![feature(bool_to_option)]
230 #![feature(box_syntax)]
231 #![feature(c_variadic)]
232 #![feature(cfg_accessible)]
233 #![feature(cfg_target_has_atomic)]
234 #![feature(cfg_target_thread_local)]
235 #![feature(char_error_internals)]
236 #![feature(char_internals)]
237 #![feature(clamp)]
238 #![feature(concat_idents)]
239 #![feature(const_cstr_unchecked)]
240 #![cfg_attr(not(bootstrap), feature(const_fn_floating_point_arithmetic))]
241 #![feature(const_fn_transmute)]
242 #![feature(const_fn)]
243 #![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))]
244 #![feature(const_ip)]
245 #![feature(const_ipv6)]
246 #![feature(const_raw_ptr_deref)]
247 #![feature(const_ipv4)]
248 #![feature(container_error_extra)]
249 #![feature(core_intrinsics)]
250 #![feature(custom_test_frameworks)]
251 #![feature(decl_macro)]
252 #![cfg_attr(bootstrap, feature(doc_alias))]
253 #![feature(doc_cfg)]
254 #![feature(doc_keyword)]
255 #![feature(doc_masked)]
256 #![feature(doc_spotlight)]
257 #![feature(dropck_eyepatch)]
258 #![feature(duration_constants)]
259 #![feature(exact_size_is_empty)]
260 #![feature(exhaustive_patterns)]
261 #![feature(extend_one)]
262 #![feature(external_doc)]
263 #![feature(fn_traits)]
264 #![feature(format_args_nl)]
265 #![feature(gen_future)]
266 #![feature(generator_trait)]
267 #![feature(global_asm)]
268 #![feature(hashmap_internals)]
269 #![feature(int_error_internals)]
270 #![feature(int_error_matching)]
271 #![feature(integer_atomics)]
272 #![feature(into_future)]
273 #![feature(lang_items)]
274 #![feature(link_args)]
275 #![feature(linkage)]
276 #![feature(llvm_asm)]
277 #![feature(log_syntax)]
278 #![feature(maybe_uninit_extra)]
279 #![feature(maybe_uninit_ref)]
280 #![feature(maybe_uninit_slice)]
281 #![feature(min_specialization)]
282 #![feature(needs_panic_runtime)]
283 #![feature(negative_impls)]
284 #![feature(never_type)]
285 #![feature(nll)]
286 #![feature(nonnull_slice_from_raw_parts)]
287 #![feature(once_cell)]
288 #![feature(optin_builtin_traits)]
289 #![feature(or_patterns)]
290 #![feature(panic_info_message)]
291 #![feature(panic_internals)]
292 #![feature(panic_unwind)]
293 #![feature(prelude_import)]
294 #![feature(ptr_internals)]
295 #![feature(raw)]
296 #![feature(raw_ref_macros)]
297 #![feature(ready_macro)]
298 #![feature(renamed_spin_loop)]
299 #![feature(rustc_attrs)]
300 #![feature(rustc_private)]
301 #![feature(shrink_to)]
302 #![feature(slice_concat_ext)]
303 #![feature(slice_internals)]
304 #![feature(slice_ptr_get)]
305 #![feature(slice_ptr_len)]
306 #![feature(slice_strip)]
307 #![feature(staged_api)]
308 #![feature(std_internals)]
309 #![feature(stdsimd)]
310 #![feature(stmt_expr_attributes)]
311 #![feature(str_internals)]
312 #![feature(test)]
313 #![feature(thread_local)]
314 #![feature(thread_local_internals)]
315 #![feature(toowned_clone_into)]
316 #![feature(total_cmp)]
317 #![feature(trace_macros)]
318 #![feature(try_reserve)]
319 #![feature(unboxed_closures)]
320 #![feature(unsafe_block_in_unsafe_fn)]
321 #![feature(unsafe_cell_get_mut)]
322 #![feature(unsafe_cell_raw_get)]
323 #![feature(untagged_unions)]
324 #![feature(unwind_attributes)]
325 #![feature(vec_into_raw_parts)]
326 #![feature(wake_trait)]
327 // NB: the above list is sorted to minimize merge conflicts.
328 #![default_lib_allocator]
329
330 // Explicitly import the prelude. The compiler uses this same unstable attribute
331 // to import the prelude implicitly when building crates that depend on std.
332 #[prelude_import]
333 #[allow(unused)]
334 use prelude::v1::*;
335
336 // Access to Bencher, etc.
337 #[cfg(test)]
338 extern crate test;
339
340 #[allow(unused_imports)] // macros from `alloc` are not used on all platforms
341 #[macro_use]
342 extern crate alloc as alloc_crate;
343 #[doc(masked)]
344 #[allow(unused_extern_crates)]
345 extern crate libc;
346
347 // We always need an unwinder currently for backtraces
348 #[doc(masked)]
349 #[allow(unused_extern_crates)]
350 extern crate unwind;
351
352 // During testing, this crate is not actually the "real" std library, but rather
353 // it links to the real std library, which was compiled from this same source
354 // code. So any lang items std defines are conditionally excluded (or else they
355 // would generate duplicate lang item errors), and any globals it defines are
356 // _not_ the globals used by "real" std. So this import, defined only during
357 // testing gives test-std access to real-std lang items and globals. See #2912
358 #[cfg(test)]
359 extern crate std as realstd;
360
361 // The standard macros that are not built-in to the compiler.
362 #[macro_use]
363 mod macros;
364
365 // The Rust prelude
366 pub mod prelude;
367
368 // Public module declarations and re-exports
369 #[stable(feature = "rust1", since = "1.0.0")]
370 pub use alloc_crate::borrow;
371 #[stable(feature = "rust1", since = "1.0.0")]
372 pub use alloc_crate::boxed;
373 #[stable(feature = "rust1", since = "1.0.0")]
374 pub use alloc_crate::fmt;
375 #[stable(feature = "rust1", since = "1.0.0")]
376 pub use alloc_crate::format;
377 #[stable(feature = "rust1", since = "1.0.0")]
378 pub use alloc_crate::rc;
379 #[stable(feature = "rust1", since = "1.0.0")]
380 pub use alloc_crate::slice;
381 #[stable(feature = "rust1", since = "1.0.0")]
382 pub use alloc_crate::str;
383 #[stable(feature = "rust1", since = "1.0.0")]
384 pub use alloc_crate::string;
385 #[stable(feature = "rust1", since = "1.0.0")]
386 pub use alloc_crate::vec;
387 #[stable(feature = "rust1", since = "1.0.0")]
388 pub use core::any;
389 #[stable(feature = "simd_arch", since = "1.27.0")]
390 #[doc(no_inline)]
391 pub use core::arch;
392 #[stable(feature = "core_array", since = "1.36.0")]
393 pub use core::array;
394 #[stable(feature = "rust1", since = "1.0.0")]
395 pub use core::cell;
396 #[stable(feature = "rust1", since = "1.0.0")]
397 pub use core::char;
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub use core::clone;
400 #[stable(feature = "rust1", since = "1.0.0")]
401 pub use core::cmp;
402 #[stable(feature = "rust1", since = "1.0.0")]
403 pub use core::convert;
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub use core::default;
406 #[stable(feature = "rust1", since = "1.0.0")]
407 pub use core::hash;
408 #[stable(feature = "core_hint", since = "1.27.0")]
409 pub use core::hint;
410 #[stable(feature = "i128", since = "1.26.0")]
411 pub use core::i128;
412 #[stable(feature = "rust1", since = "1.0.0")]
413 pub use core::i16;
414 #[stable(feature = "rust1", since = "1.0.0")]
415 pub use core::i32;
416 #[stable(feature = "rust1", since = "1.0.0")]
417 pub use core::i64;
418 #[stable(feature = "rust1", since = "1.0.0")]
419 pub use core::i8;
420 #[stable(feature = "rust1", since = "1.0.0")]
421 pub use core::intrinsics;
422 #[stable(feature = "rust1", since = "1.0.0")]
423 pub use core::isize;
424 #[stable(feature = "rust1", since = "1.0.0")]
425 pub use core::iter;
426 #[stable(feature = "rust1", since = "1.0.0")]
427 pub use core::marker;
428 #[stable(feature = "rust1", since = "1.0.0")]
429 pub use core::mem;
430 #[stable(feature = "rust1", since = "1.0.0")]
431 pub use core::ops;
432 #[stable(feature = "rust1", since = "1.0.0")]
433 pub use core::option;
434 #[stable(feature = "pin", since = "1.33.0")]
435 pub use core::pin;
436 #[stable(feature = "rust1", since = "1.0.0")]
437 pub use core::ptr;
438 #[stable(feature = "rust1", since = "1.0.0")]
439 pub use core::raw;
440 #[stable(feature = "rust1", since = "1.0.0")]
441 pub use core::result;
442 #[stable(feature = "i128", since = "1.26.0")]
443 pub use core::u128;
444 #[stable(feature = "rust1", since = "1.0.0")]
445 pub use core::u16;
446 #[stable(feature = "rust1", since = "1.0.0")]
447 pub use core::u32;
448 #[stable(feature = "rust1", since = "1.0.0")]
449 pub use core::u64;
450 #[stable(feature = "rust1", since = "1.0.0")]
451 pub use core::u8;
452 #[stable(feature = "rust1", since = "1.0.0")]
453 pub use core::usize;
454
455 pub mod f32;
456 pub mod f64;
457
458 #[macro_use]
459 pub mod thread;
460 pub mod ascii;
461 pub mod backtrace;
462 pub mod collections;
463 pub mod env;
464 pub mod error;
465 pub mod ffi;
466 pub mod fs;
467 pub mod io;
468 pub mod net;
469 pub mod num;
470 pub mod os;
471 pub mod panic;
472 pub mod path;
473 pub mod process;
474 pub mod sync;
475 pub mod time;
476
477 #[unstable(feature = "once_cell", issue = "74465")]
478 pub mod lazy;
479
480 #[stable(feature = "futures_api", since = "1.36.0")]
481 pub mod task {
482 //! Types and Traits for working with asynchronous tasks.
483
484 #[doc(inline)]
485 #[stable(feature = "futures_api", since = "1.36.0")]
486 pub use core::task::*;
487
488 #[doc(inline)]
489 #[unstable(feature = "wake_trait", issue = "69912")]
490 pub use alloc::task::*;
491 }
492
493 #[stable(feature = "futures_api", since = "1.36.0")]
494 pub mod future;
495
496 // Platform-abstraction modules
497 #[macro_use]
498 mod sys_common;
499 mod sys;
500
501 pub mod alloc;
502
503 // Private support modules
504 mod memchr;
505 mod panicking;
506
507 // The runtime entry point and a few unstable public functions used by the
508 // compiler
509 pub mod rt;
510
511 #[path = "../../backtrace/src/lib.rs"]
512 #[allow(dead_code, unused_attributes)]
513 mod backtrace_rs;
514
515 // Pull in the `std_detect` crate directly into libstd. The contents of
516 // `std_detect` are in a different repository: rust-lang/stdarch.
517 //
518 // `std_detect` depends on libstd, but the contents of this module are
519 // set up in such a way that directly pulling it here works such that the
520 // crate uses the this crate as its libstd.
521 #[path = "../../stdarch/crates/std_detect/src/mod.rs"]
522 #[allow(missing_debug_implementations, missing_docs, dead_code)]
523 #[unstable(feature = "stdsimd", issue = "48556")]
524 #[cfg(not(test))]
525 mod std_detect;
526
527 #[doc(hidden)]
528 #[unstable(feature = "stdsimd", issue = "48556")]
529 #[cfg(not(test))]
530 pub use std_detect::detect;
531
532 // Re-export macros defined in libcore.
533 #[stable(feature = "rust1", since = "1.0.0")]
534 #[allow(deprecated, deprecated_in_future)]
535 pub use core::{
536 assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, matches, r#try, todo,
537 unimplemented, unreachable, write, writeln,
538 };
539
540 // Re-export built-in macros defined through libcore.
541 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
542 #[allow(deprecated)]
543 pub use core::{
544 asm, assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args,
545 format_args_nl, global_asm, include, include_bytes, include_str, line, llvm_asm, log_syntax,
546 module_path, option_env, stringify, trace_macros,
547 };
548
549 #[stable(feature = "core_primitive", since = "1.43.0")]
550 pub use core::primitive;
551
552 // Include a number of private modules that exist solely to provide
553 // the rustdoc documentation for primitive types. Using `include!`
554 // because rustdoc only looks for these modules at the crate level.
555 include!("primitive_docs.rs");
556
557 // Include a number of private modules that exist solely to provide
558 // the rustdoc documentation for the existing keywords. Using `include!`
559 // because rustdoc only looks for these modules at the crate level.
560 include!("keyword_docs.rs");
561
562 // This is required to avoid an unstable error when `restricted-std` is not
563 // enabled. The use of #![feature(restricted_std)] in rustc-std-workspace-std
564 // is unconditional, so the unstable feature needs to be defined somewhere.
565 #[cfg_attr(not(feature = "restricted-std"), unstable(feature = "restricted_std", issue = "none"))]
566 mod __restricted_std_workaround {}