]> git.lizzy.rs Git - rust.git/blob - src/libstd/lib.rs
Add x86_64-fortanix-unknown-sgx target to libstd and dependencies
[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/ch03-05-control-flow.html#looping-through-a-collection-with-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/ch07-02-modules-and-use-to-control-scope-and-privacy.html#the-use-keyword-to-bring-paths-into-a-scope
204 //! [crate root]: ../book/ch07-01-packages-and-crates-for-making-libraries-and-executables.html
205 //! [crates.io]: https://crates.io
206 //! [deref-coercions]: ../book/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/ch03-02-data-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(allocator_api)]
239 #![feature(allocator_internals)]
240 #![feature(allow_internal_unsafe)]
241 #![feature(allow_internal_unstable)]
242 #![feature(align_offset)]
243 #![feature(arbitrary_self_types)]
244 #![feature(array_error_internals)]
245 #![feature(asm)]
246 #![feature(box_syntax)]
247 #![feature(c_variadic)]
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(const_int_ops)]
254 #![feature(const_ip)]
255 #![feature(const_raw_ptr_deref)]
256 #![feature(const_cstr_unchecked)]
257 #![feature(core_intrinsics)]
258 #![feature(dropck_eyepatch)]
259 #![feature(duration_as_u128)]
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(hash_raw_entry)]
287 #![feature(rustc_attrs)]
288 #![feature(rustc_const_unstable)]
289 #![feature(std_internals)]
290 #![cfg_attr(not(stage0), feature(stdsimd))]
291 #![feature(shrink_to)]
292 #![feature(slice_concat_ext)]
293 #![feature(slice_internals)]
294 #![feature(slice_patterns)]
295 #![feature(staged_api)]
296 #![feature(stmt_expr_attributes)]
297 #![feature(str_internals)]
298 #![feature(rustc_private)]
299 #![feature(thread_local)]
300 #![feature(toowned_clone_into)]
301 #![feature(try_from)]
302 #![feature(try_reserve)]
303 #![feature(unboxed_closures)]
304 #![feature(untagged_unions)]
305 #![feature(unwind_attributes)]
306 #![feature(doc_cfg)]
307 #![feature(doc_masked)]
308 #![feature(doc_spotlight)]
309 #![feature(doc_alias)]
310 #![feature(doc_keyword)]
311 #![feature(panic_info_message)]
312 #![feature(non_exhaustive)]
313 #![feature(alloc_layout_extra)]
314 #![feature(maybe_uninit)]
315 #![cfg_attr(target_env = "sgx", feature(global_asm, range_contains))]
316
317 #![default_lib_allocator]
318
319 #[cfg(stage0)]
320 #[global_allocator]
321 static ALLOC: alloc::System = alloc::System;
322
323 // Explicitly import the prelude. The compiler uses this same unstable attribute
324 // to import the prelude implicitly when building crates that depend on std.
325 #[prelude_import]
326 #[allow(unused)]
327 use prelude::v1::*;
328
329 // Access to Bencher, etc.
330 #[cfg(test)] extern crate test;
331 #[cfg(test)] extern crate rand;
332
333 // Re-export a few macros from core
334 #[stable(feature = "rust1", since = "1.0.0")]
335 pub use core::{assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne};
336 #[stable(feature = "rust1", since = "1.0.0")]
337 pub use core::{unreachable, unimplemented, write, writeln, try};
338
339 #[allow(unused_imports)] // macros from `alloc` are not used on all platforms
340 #[macro_use]
341 extern crate alloc as alloc_crate;
342 #[doc(masked)]
343 extern crate libc;
344
345 // We always need an unwinder currently for backtraces
346 #[doc(masked)]
347 #[allow(unused_extern_crates)]
348 extern crate unwind;
349
350 // During testing, this crate is not actually the "real" std library, but rather
351 // it links to the real std library, which was compiled from this same source
352 // code. So any lang items std defines are conditionally excluded (or else they
353 // would generate duplicate lang item errors), and any globals it defines are
354 // _not_ the globals used by "real" std. So this import, defined only during
355 // testing gives test-std access to real-std lang items and globals. See #2912
356 #[cfg(test)] extern crate std as realstd;
357
358 #[cfg(target_env = "sgx")]
359 #[macro_use]
360 #[allow(unused_imports)] // FIXME: without `#[macro_use]`, get error: “cannot
361                          // determine resolution for the macro `usercalls_asm`”
362 extern crate fortanix_sgx_abi;
363
364 // The standard macros that are not built-in to the compiler.
365 #[macro_use]
366 mod macros;
367
368 // The Rust prelude
369 pub mod prelude;
370
371 // Public module declarations and re-exports
372 #[stable(feature = "rust1", since = "1.0.0")]
373 pub use core::any;
374 #[stable(feature = "rust1", since = "1.0.0")]
375 pub use core::cell;
376 #[stable(feature = "rust1", since = "1.0.0")]
377 pub use core::clone;
378 #[stable(feature = "rust1", since = "1.0.0")]
379 pub use core::cmp;
380 #[stable(feature = "rust1", since = "1.0.0")]
381 pub use core::convert;
382 #[stable(feature = "rust1", since = "1.0.0")]
383 pub use core::default;
384 #[stable(feature = "rust1", since = "1.0.0")]
385 pub use core::hash;
386 #[stable(feature = "rust1", since = "1.0.0")]
387 pub use core::intrinsics;
388 #[stable(feature = "rust1", since = "1.0.0")]
389 pub use core::iter;
390 #[stable(feature = "rust1", since = "1.0.0")]
391 pub use core::marker;
392 #[stable(feature = "rust1", since = "1.0.0")]
393 pub use core::mem;
394 #[stable(feature = "rust1", since = "1.0.0")]
395 pub use core::ops;
396 #[stable(feature = "rust1", since = "1.0.0")]
397 pub use core::ptr;
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub use core::raw;
400 #[stable(feature = "rust1", since = "1.0.0")]
401 pub use core::result;
402 #[stable(feature = "rust1", since = "1.0.0")]
403 pub use core::option;
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub use core::isize;
406 #[stable(feature = "rust1", since = "1.0.0")]
407 pub use core::i8;
408 #[stable(feature = "rust1", since = "1.0.0")]
409 pub use core::i16;
410 #[stable(feature = "rust1", since = "1.0.0")]
411 pub use core::i32;
412 #[stable(feature = "rust1", since = "1.0.0")]
413 pub use core::i64;
414 #[stable(feature = "i128", since = "1.26.0")]
415 pub use core::i128;
416 #[stable(feature = "rust1", since = "1.0.0")]
417 pub use core::usize;
418 #[stable(feature = "rust1", since = "1.0.0")]
419 pub use core::u8;
420 #[stable(feature = "rust1", since = "1.0.0")]
421 pub use core::u16;
422 #[stable(feature = "rust1", since = "1.0.0")]
423 pub use core::u32;
424 #[stable(feature = "rust1", since = "1.0.0")]
425 pub use core::u64;
426 #[stable(feature = "rust1", since = "1.0.0")]
427 pub use alloc_crate::boxed;
428 #[stable(feature = "rust1", since = "1.0.0")]
429 pub use alloc_crate::rc;
430 #[stable(feature = "rust1", since = "1.0.0")]
431 pub use alloc_crate::borrow;
432 #[stable(feature = "rust1", since = "1.0.0")]
433 pub use alloc_crate::fmt;
434 #[stable(feature = "rust1", since = "1.0.0")]
435 pub use alloc_crate::format;
436 #[unstable(feature = "pin", issue = "49150")]
437 pub use core::pin;
438 #[stable(feature = "rust1", since = "1.0.0")]
439 pub use alloc_crate::slice;
440 #[stable(feature = "rust1", since = "1.0.0")]
441 pub use alloc_crate::str;
442 #[stable(feature = "rust1", since = "1.0.0")]
443 pub use alloc_crate::string;
444 #[stable(feature = "rust1", since = "1.0.0")]
445 pub use alloc_crate::vec;
446 #[stable(feature = "rust1", since = "1.0.0")]
447 pub use core::char;
448 #[stable(feature = "i128", since = "1.26.0")]
449 pub use core::u128;
450 #[stable(feature = "core_hint", since = "1.27.0")]
451 pub use core::hint;
452
453 pub mod f32;
454 pub mod f64;
455
456 #[macro_use]
457 pub mod thread;
458 pub mod ascii;
459 pub mod collections;
460 pub mod env;
461 pub mod error;
462 pub mod ffi;
463 pub mod fs;
464 pub mod io;
465 pub mod net;
466 pub mod num;
467 pub mod os;
468 pub mod panic;
469 pub mod path;
470 pub mod process;
471 pub mod sync;
472 pub mod time;
473
474 #[unstable(feature = "futures_api",
475            reason = "futures in libcore are unstable",
476            issue = "50547")]
477 pub mod task {
478     //! Types and Traits for working with asynchronous tasks.
479     #[doc(inline)]
480     pub use core::task::*;
481     #[doc(inline)]
482     pub use alloc_crate::task::*;
483 }
484
485 #[unstable(feature = "futures_api",
486            reason = "futures in libcore are unstable",
487            issue = "50547")]
488 pub mod future;
489
490 // Platform-abstraction modules
491 #[macro_use]
492 mod sys_common;
493 mod sys;
494
495 pub mod alloc;
496
497 // Private support modules
498 mod panicking;
499 mod memchr;
500
501 // The runtime entry point and a few unstable public functions used by the
502 // compiler
503 pub mod rt;
504
505 // Pull in the `stdsimd` crate directly into libstd. This is the same as
506 // libcore's arch/simd modules where the source of truth here is in a different
507 // repository, but we pull things in here manually to get it into libstd.
508 //
509 // Note that the #[cfg] here is intended to do two things. First it allows us to
510 // change the rustc implementation of intrinsics in stage0 by not compiling simd
511 // intrinsics in stage0. Next it doesn't compile anything in test mode as
512 // stdsimd has tons of its own tests which we don't want to run.
513 #[path = "../stdsimd/stdsimd/mod.rs"]
514 #[allow(missing_debug_implementations, missing_docs, dead_code)]
515 #[unstable(feature = "stdsimd", issue = "48556")]
516 #[cfg(all(not(stage0), not(test)))]
517 mod stdsimd;
518
519 // A "fake" module needed by the `stdsimd` module to compile, not actually
520 // exported though.
521 #[cfg(not(stage0))]
522 mod coresimd {
523     pub use core::arch;
524 }
525
526 #[stable(feature = "simd_arch", since = "1.27.0")]
527 #[cfg(all(not(stage0), not(test)))]
528 pub use stdsimd::arch;
529
530 // Include a number of private modules that exist solely to provide
531 // the rustdoc documentation for primitive types. Using `include!`
532 // because rustdoc only looks for these modules at the crate level.
533 include!("primitive_docs.rs");
534
535 // Include a number of private modules that exist solely to provide
536 // the rustdoc documentation for the existing keywords. Using `include!`
537 // because rustdoc only looks for these modules at the crate level.
538 include!("keyword_docs.rs");