]> git.lizzy.rs Git - rust.git/blob - src/libstd/lib.rs
make `CStr::from_bytes_with_nul_unchecked()` a const fn
[rust.git] / src / libstd / lib.rs
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.
4 //
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.
10
11 //! # The Rust Standard Library
12 //!
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].
19 //!
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`].
25 //!
26 //! # How to read this documentation
27 //!
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.
31 //!
32 //! Otherwise, you may want to jump to one of these useful sections:
33 //!
34 //! * [`std::*` modules](#modules)
35 //! * [Primitive types](#primitives)
36 //! * [Standard macros](#macros)
37 //! * [The Rust Prelude](prelude/index.html)
38 //!
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!
44 //!
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.
49 //!
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.
54 //!
55 //! # What is in the standard library documentation?
56 //!
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.
63 //!
64 //! Second, implicit methods on [primitive types] are documented here. This can
65 //! be a source of confusion for two reasons:
66 //!
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.
74 //!
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).
80 //!
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].
85 //!
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.
90 //!
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.
96 //!
97 //! # Contributing changes to the documentation
98 //!
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.
104 //!
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.
107 //!
108 //! # A Tour of The Rust Standard Library
109 //!
110 //! The rest of this crate documentation is dedicated to pointing out notable
111 //! features of The Rust Standard Library.
112 //!
113 //! ## Containers and collections
114 //!
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.
119 //!
120 //! The standard library exposes three common ways to deal with contiguous
121 //! regions of memory:
122 //!
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.
127 //!
128 //! Slices can only be handled through some kind of *pointer*, and as such come
129 //! in many flavors such as:
130 //!
131 //! * `&[T]` - *shared slice*
132 //! * `&mut [T]` - *mutable slice*
133 //! * [`Box<[T]>`][owned slice] - *owned slice*
134 //!
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.
139 //!
140 //! For converting to strings use the [`format!`] macro, and for converting from
141 //! strings use the [`FromStr`] trait.
142 //!
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
147 //! effect.
148 //!
149 //! The [`collections`] module defines maps, sets, linked lists and other
150 //! typical collection types, including the common [`HashMap<K, V>`].
151 //!
152 //! ## Platform abstractions and I/O
153 //!
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.
157 //!
158 //! Common types of I/O, including [files], [TCP], [UDP], are defined in the
159 //! [`io`], [`fs`], and [`net`] modules.
160 //!
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.
164 //!
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
211
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))))]
220
221 // Don't link to std. We are std.
222 #![no_std]
223
224 #![deny(missing_docs)]
225 #![deny(missing_debug_implementations)]
226
227 // Tell the compiler to link to either panic_abort or panic_unwind
228 #![needs_panic_runtime]
229
230 // std may use features in a platform-specific way
231 #![allow(unused_features)]
232
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))]
236 #![feature(alloc)]
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)]
246 #![feature(asm)]
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 #![feature(min_const_fn)]
254 #![feature(const_int_ops)]
255 #![feature(const_ip)]
256 #![feature(const_raw_ptr_deref)]
257 #![feature(const_cstr_unchecked)]
258 #![feature(core_intrinsics)]
259 #![feature(dropck_eyepatch)]
260 #![feature(exact_size_is_empty)]
261 #![feature(external_doc)]
262 #![feature(fixed_size_array)]
263 #![feature(fn_traits)]
264 #![feature(fnbox)]
265 #![feature(futures_api)]
266 #![feature(generator_trait)]
267 #![feature(hashmap_internals)]
268 #![feature(int_error_internals)]
269 #![feature(integer_atomics)]
270 #![feature(lang_items)]
271 #![feature(libc)]
272 #![feature(link_args)]
273 #![feature(linkage)]
274 #![feature(needs_panic_runtime)]
275 #![feature(never_type)]
276 #![feature(nll)]
277 #![feature(exhaustive_patterns)]
278 #![feature(on_unimplemented)]
279 #![feature(optin_builtin_traits)]
280 #![feature(panic_internals)]
281 #![feature(panic_unwind)]
282 #![feature(pin)]
283 #![feature(prelude_import)]
284 #![feature(ptr_internals)]
285 #![feature(raw)]
286 #![feature(rustc_attrs)]
287 #![feature(rustc_const_unstable)]
288 #![feature(std_internals)]
289 #![cfg_attr(not(stage0), feature(stdsimd))]
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 #![feature(doc_cfg)]
306 #![feature(doc_masked)]
307 #![feature(doc_spotlight)]
308 #![feature(doc_alias)]
309 #![feature(doc_keyword)]
310 #![feature(panic_info_message)]
311 #![feature(non_exhaustive)]
312
313 #![default_lib_allocator]
314
315 // Always use alloc_system during stage0 since we don't know if the alloc_*
316 // crate the stage0 compiler will pick by default is enabled (e.g.
317 // if the user has disabled jemalloc in `./configure`).
318 // `force_alloc_system` is *only* intended as a workaround for local rebuilds
319 // with a rustc without jemalloc.
320 // FIXME(#44236) shouldn't need MSVC logic
321 #[cfg(all(not(target_env = "msvc"),
322           any(all(stage0, not(test)), feature = "force_alloc_system")))]
323 #[global_allocator]
324 static ALLOC: alloc_system::System = alloc_system::System;
325
326 // Explicitly import the prelude. The compiler uses this same unstable attribute
327 // to import the prelude implicitly when building crates that depend on std.
328 #[prelude_import]
329 #[allow(unused)]
330 use prelude::v1::*;
331
332 // Access to Bencher, etc.
333 #[cfg(test)] extern crate test;
334 #[cfg(test)] extern crate rand;
335
336 // Re-export a few macros from core
337 #[stable(feature = "rust1", since = "1.0.0")]
338 pub use core::{assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne};
339 #[stable(feature = "rust1", since = "1.0.0")]
340 pub use core::{unreachable, unimplemented, write, writeln, try};
341
342 #[allow(unused_imports)] // macros from `alloc` are not used on all platforms
343 #[macro_use]
344 extern crate alloc as alloc_crate;
345 extern crate alloc_system;
346 #[doc(masked)]
347 extern crate libc;
348
349 // We always need an unwinder currently for backtraces
350 #[doc(masked)]
351 #[allow(unused_extern_crates)]
352 extern crate unwind;
353
354 // During testing, this crate is not actually the "real" std library, but rather
355 // it links to the real std library, which was compiled from this same source
356 // code. So any lang items std defines are conditionally excluded (or else they
357 // would generate duplicate lang item errors), and any globals it defines are
358 // _not_ the globals used by "real" std. So this import, defined only during
359 // testing gives test-std access to real-std lang items and globals. See #2912
360 #[cfg(test)] extern crate std as realstd;
361
362 // The standard macros that are not built-in to the compiler.
363 #[macro_use]
364 mod macros;
365
366 // The Rust prelude
367 pub mod prelude;
368
369 // Public module declarations and re-exports
370 #[stable(feature = "rust1", since = "1.0.0")]
371 pub use core::any;
372 #[stable(feature = "rust1", since = "1.0.0")]
373 pub use core::cell;
374 #[stable(feature = "rust1", since = "1.0.0")]
375 pub use core::clone;
376 #[stable(feature = "rust1", since = "1.0.0")]
377 pub use core::cmp;
378 #[stable(feature = "rust1", since = "1.0.0")]
379 pub use core::convert;
380 #[stable(feature = "rust1", since = "1.0.0")]
381 pub use core::default;
382 #[stable(feature = "rust1", since = "1.0.0")]
383 pub use core::hash;
384 #[stable(feature = "rust1", since = "1.0.0")]
385 pub use core::intrinsics;
386 #[stable(feature = "rust1", since = "1.0.0")]
387 pub use core::iter;
388 #[stable(feature = "rust1", since = "1.0.0")]
389 pub use core::marker;
390 #[stable(feature = "rust1", since = "1.0.0")]
391 pub use core::mem;
392 #[stable(feature = "rust1", since = "1.0.0")]
393 pub use core::ops;
394 #[stable(feature = "rust1", since = "1.0.0")]
395 pub use core::ptr;
396 #[stable(feature = "rust1", since = "1.0.0")]
397 pub use core::raw;
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub use core::result;
400 #[stable(feature = "rust1", since = "1.0.0")]
401 pub use core::option;
402 #[stable(feature = "rust1", since = "1.0.0")]
403 pub use core::isize;
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub use core::i8;
406 #[stable(feature = "rust1", since = "1.0.0")]
407 pub use core::i16;
408 #[stable(feature = "rust1", since = "1.0.0")]
409 pub use core::i32;
410 #[stable(feature = "rust1", since = "1.0.0")]
411 pub use core::i64;
412 #[stable(feature = "i128", since = "1.26.0")]
413 pub use core::i128;
414 #[stable(feature = "rust1", since = "1.0.0")]
415 pub use core::usize;
416 #[stable(feature = "rust1", since = "1.0.0")]
417 pub use core::u8;
418 #[stable(feature = "rust1", since = "1.0.0")]
419 pub use core::u16;
420 #[stable(feature = "rust1", since = "1.0.0")]
421 pub use core::u32;
422 #[stable(feature = "rust1", since = "1.0.0")]
423 pub use core::u64;
424 #[stable(feature = "rust1", since = "1.0.0")]
425 pub use alloc_crate::boxed;
426 #[stable(feature = "rust1", since = "1.0.0")]
427 pub use alloc_crate::rc;
428 #[stable(feature = "rust1", since = "1.0.0")]
429 pub use alloc_crate::borrow;
430 #[stable(feature = "rust1", since = "1.0.0")]
431 pub use alloc_crate::fmt;
432 #[stable(feature = "rust1", since = "1.0.0")]
433 pub use alloc_crate::format;
434 #[unstable(feature = "pin", issue = "49150")]
435 pub use core::pin;
436 #[stable(feature = "rust1", since = "1.0.0")]
437 pub use alloc_crate::slice;
438 #[stable(feature = "rust1", since = "1.0.0")]
439 pub use alloc_crate::str;
440 #[stable(feature = "rust1", since = "1.0.0")]
441 pub use alloc_crate::string;
442 #[stable(feature = "rust1", since = "1.0.0")]
443 pub use alloc_crate::vec;
444 #[stable(feature = "rust1", since = "1.0.0")]
445 pub use core::char;
446 #[stable(feature = "i128", since = "1.26.0")]
447 pub use core::u128;
448 #[stable(feature = "core_hint", since = "1.27.0")]
449 pub use core::hint;
450
451 pub mod f32;
452 pub mod f64;
453
454 #[macro_use]
455 pub mod thread;
456 pub mod ascii;
457 pub mod collections;
458 pub mod env;
459 pub mod error;
460 pub mod ffi;
461 pub mod fs;
462 pub mod io;
463 pub mod net;
464 pub mod num;
465 pub mod os;
466 pub mod panic;
467 pub mod path;
468 pub mod process;
469 pub mod sync;
470 pub mod time;
471
472 #[unstable(feature = "futures_api",
473            reason = "futures in libcore are unstable",
474            issue = "50547")]
475 pub mod task {
476     //! Types and Traits for working with asynchronous tasks.
477     #[doc(inline)]
478     pub use core::task::*;
479     #[doc(inline)]
480     pub use alloc_crate::task::*;
481 }
482
483 #[unstable(feature = "futures_api",
484            reason = "futures in libcore are unstable",
485            issue = "50547")]
486 pub mod future;
487
488 // Platform-abstraction modules
489 #[macro_use]
490 mod sys_common;
491 mod sys;
492
493 pub mod alloc;
494
495 // Private support modules
496 mod panicking;
497 mod memchr;
498
499 // The runtime entry point and a few unstable public functions used by the
500 // compiler
501 pub mod rt;
502
503 // Pull in the the `stdsimd` crate directly into libstd. This is the same as
504 // libcore's arch/simd modules where the source of truth here is in a different
505 // repository, but we pull things in here manually to get it into libstd.
506 //
507 // Note that the #[cfg] here is intended to do two things. First it allows us to
508 // change the rustc implementation of intrinsics in stage0 by not compiling simd
509 // intrinsics in stage0. Next it doesn't compile anything in test mode as
510 // stdsimd has tons of its own tests which we don't want to run.
511 #[path = "../stdsimd/stdsimd/mod.rs"]
512 #[allow(missing_debug_implementations, missing_docs, dead_code)]
513 #[unstable(feature = "stdsimd", issue = "48556")]
514 #[cfg(all(not(stage0), not(test)))]
515 mod stdsimd;
516
517 // A "fake" module needed by the `stdsimd` module to compile, not actually
518 // exported though.
519 #[cfg(not(stage0))]
520 mod coresimd {
521     pub use core::arch;
522 }
523
524 #[stable(feature = "simd_arch", since = "1.27.0")]
525 #[cfg(all(not(stage0), not(test)))]
526 pub use stdsimd::arch;
527
528 // Include a number of private modules that exist solely to provide
529 // the rustdoc documentation for primitive types. Using `include!`
530 // because rustdoc only looks for these modules at the crate level.
531 include!("primitive_docs.rs");
532
533 // Include a number of private modules that exist solely to provide
534 // the rustdoc documentation for the existing keywords. Using `include!`
535 // because rustdoc only looks for these modules at the crate level.
536 include!("keyword_docs.rs");