]> git.proxmox.com Git - rustc.git/blame - src/libstd/lib.rs
New upstream version 1.12.0+dfsg1
[rustc.git] / src / libstd / lib.rs
CommitLineData
1a4d82fc
JJ
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//!
92a42be0
SL
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()`].
1a4d82fc 25//!
c1a9b12d 26//! # How to read this documentation
1a4d82fc 27//!
9cc50fc6 28//! If you already know the name of what you are looking for, the fastest way to
92a42be0
SL
29//! find it is to use the <a href="#" onclick="focusSearchBar();">search
30//! bar</a> at the top of the page.
1a4d82fc 31//!
c1a9b12d
SL
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//!
92a42be0
SL
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!
c1a9b12d 44//!
92a42be0
SL
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.
c1a9b12d 49//!
92a42be0
SL
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.
c1a9b12d
SL
54//!
55//! # What is in the standard library documentation?
56//!
92a42be0
SL
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.
c1a9b12d 63//!
92a42be0 64//! Second, implicit methods on [primitive types] are documented here. This can
c1a9b12d
SL
65//! be a source of confusion for two reasons:
66//!
92a42be0
SL
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.
c1a9b12d
SL
74//!
75//! So for example there is a [page for the primitive type
92a42be0
SL
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].
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
c1a9b12d
SL
89//! documentation a good entry point to learning about the library.
90//!
92a42be0
SL
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.
c1a9b12d
SL
96//!
97//! # A Tour of The Rust Standard Library
98//!
92a42be0
SL
99//! The rest of this crate documentation is dedicated to pointing out notable
100//! features of The Rust Standard Library.
1a4d82fc 101//!
bd371182 102//! ## Containers and collections
1a4d82fc 103//!
92a42be0
SL
104//! The [`option`] and [`result`] modules define optional and error-handling
105//! types, [`Option<T>`] and [`Result<T, E>`]. The [`iter`] module defines
106//! Rust's iterator trait, [`Iterator`], which works with the [`for`] loop to
107//! access collections.
1a4d82fc 108//!
92a42be0 109//! The standard library exposes three common ways to deal with contiguous
c1a9b12d
SL
110//! regions of memory:
111//!
92a42be0
SL
112//! * [`Vec<T>`] - A heap-allocated *vector* that is resizable at runtime.
113//! * [`[T; n]`][array] - An inline *array* with a fixed size at compile time.
114//! * [`[T]`][slice] - A dynamically sized *slice* into any other kind of contiguous
115//! storage, whether heap-allocated or not.
c1a9b12d 116//!
92a42be0
SL
117//! Slices can only be handled through some kind of *pointer*, and as such come
118//! in many flavors such as:
1a4d82fc 119//!
c1a9b12d
SL
120//! * `&[T]` - *shared slice*
121//! * `&mut [T]` - *mutable slice*
92a42be0 122//! * [`Box<[T]>`][owned slice] - *owned slice*
c1a9b12d 123//!
92a42be0
SL
124//! [`str`], a UTF-8 string slice, is a primitive type, and the standard library
125//! defines many methods for it. Rust [`str`]s are typically accessed as
126//! immutable references: `&str`. Use the owned [`String`] for building and
127//! mutating strings.
1a4d82fc 128//!
92a42be0
SL
129//! For converting to strings use the [`format!`] macro, and for converting from
130//! strings use the [`FromStr`] trait.
1a4d82fc 131//!
92a42be0
SL
132//! Data may be shared by placing it in a reference-counted box or the [`Rc`]
133//! type, and if further contained in a [`Cell`] or [`RefCell`], may be mutated
134//! as well as shared. Likewise, in a concurrent setting it is common to pair an
135//! atomically-reference-counted box, [`Arc`], with a [`Mutex`] to get the same
136//! effect.
bd371182 137//!
92a42be0
SL
138//! The [`collections`] module defines maps, sets, linked lists and other
139//! typical collection types, including the common [`HashMap<K, V>`].
bd371182
AL
140//!
141//! ## Platform abstractions and I/O
1a4d82fc 142//!
92a42be0
SL
143//! Besides basic data types, the standard library is largely concerned with
144//! abstracting over differences in common platforms, most notably Windows and
145//! Unix derivatives.
146//!
147//! Common types of I/O, including [files], [TCP], [UDP], are defined in the
148//! [`io`], [`fs`], and [`net`] modules.
149//!
150//! The [`thread`] module contains Rust's threading abstractions. [`sync`]
151//! contains further primitive shared memory types, including [`atomic`] and
152//! [`mpsc`], which contains the channel types for message passing.
153//!
154//! [I/O]: io/index.html
9cc50fc6 155//! [`MIN`]: i32/constant.MIN.html
92a42be0
SL
156//! [TCP]: net/struct.TcpStream.html
157//! [The Rust Prelude]: prelude/index.html
158//! [UDP]: net/struct.UdpSocket.html
159//! [`::std::env::args()`]: env/fn.args.html
160//! [`Arc`]: sync/struct.Arc.html
161//! [owned slice]: boxed/index.html
162//! [`Cell`]: cell/struct.Cell.html
163//! [`FromStr`]: str/trait.FromStr.html
164//! [`HashMap<K, V>`]: collections/struct.HashMap.html
165//! [`Iterator`]: iter/trait.Iterator.html
166//! [`Mutex`]: sync/struct.Mutex.html
167//! [`Option<T>`]: option/enum.Option.html
168//! [`Rc`]: rc/index.html
169//! [`RefCell`]: cell/struct.RefCell.html
170//! [`Result<T, E>`]: result/enum.Result.html
171//! [`String`]: string/struct.String.html
172//! [`Vec<T>`]: vec/index.html
173//! [array]: primitive.array.html
174//! [slice]: primitive.slice.html
175//! [`atomic`]: sync/atomic/index.html
176//! [`collections`]: collections/index.html
177//! [`for`]: ../book/loops.html#for
178//! [`format!`]: macro.format!.html
179//! [`fs`]: fs/index.html
180//! [`io`]: io/index.html
181//! [`iter`]: iter/index.html
182//! [`mpsc`]: sync/mpsc/index.html
183//! [`net`]: net/index.html
184//! [`option`]: option/index.html
185//! [`result`]: result/index.html
186//! [`std::cmp`]: cmp/index.html
187//! [`std::slice`]: slice/index.html
188//! [`str`]: primitive.str.html
189//! [`sync`]: sync/index.html
190//! [`thread`]: thread/index.html
191//! [`use std::env`]: env/index.html
192//! [`use`]: ../book/crates-and-modules.html#importing-modules-with-use
54a0048b 193//! [crate root]: ../book/crates-and-modules.html#basic-terminology-crates-and-modules
92a42be0
SL
194//! [crates.io]: https://crates.io
195//! [deref coercions]: ../book/deref-coercions.html
196//! [files]: fs/struct.File.html
197//! [multithreading]: thread/index.html
198//! [other]: #what-is-in-the-standard-library-documentation
199//! [primitive types]: ../book/primitive-types.html
bd371182 200
1a4d82fc 201#![crate_name = "std"]
85aaf69f 202#![stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
203#![crate_type = "rlib"]
204#![crate_type = "dylib"]
e9174d1e 205#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
62682a34 206 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
e9174d1e
SL
207 html_root_url = "https://doc.rust-lang.org/nightly/",
208 html_playground_url = "https://play.rust-lang.org/",
209 issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
62682a34
SL
210 test(no_crate_inject, attr(deny(warnings))),
211 test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))))]
1a4d82fc 212
3157f602
XL
213#![needs_panic_runtime]
214
85aaf69f 215#![feature(alloc)]
d9579d0f 216#![feature(allow_internal_unstable)]
92a42be0 217#![feature(asm)]
d9579d0f 218#![feature(associated_consts)]
62682a34 219#![feature(borrow_state)]
1a4d82fc 220#![feature(box_syntax)]
9cc50fc6 221#![feature(cfg_target_thread_local)]
5bcae85e
SL
222#![feature(cfg_target_vendor)]
223#![feature(char_escape_debug)]
62682a34 224#![feature(char_internals)]
85aaf69f 225#![feature(collections)]
62682a34
SL
226#![feature(collections_bound)]
227#![feature(const_fn)]
62682a34
SL
228#![feature(core_float)]
229#![feature(core_intrinsics)]
92a42be0
SL
230#![feature(dropck_parametricity)]
231#![feature(float_extras)]
232#![feature(float_from_str_radix)]
54a0048b 233#![feature(fn_traits)]
5bcae85e 234#![feature(fnbox)]
9cc50fc6 235#![feature(hashmap_hasher)]
5bcae85e 236#![feature(heap_api)]
54a0048b 237#![feature(inclusive_range)]
62682a34 238#![feature(int_error_internals)]
d9579d0f 239#![feature(into_cow)]
85aaf69f
SL
240#![feature(lang_items)]
241#![feature(libc)]
92a42be0
SL
242#![feature(link_args)]
243#![feature(linkage)]
d9579d0f 244#![feature(macro_reexport)]
54a0048b 245#![cfg_attr(test, feature(map_values_mut))]
5bcae85e 246#![feature(needs_panic_runtime)]
9cc50fc6
SL
247#![feature(num_bits_bytes)]
248#![feature(old_wrapping)]
249#![feature(on_unimplemented)]
62682a34 250#![feature(oom)]
85aaf69f 251#![feature(optin_builtin_traits)]
a7813a04 252#![feature(panic_unwind)]
c1a9b12d 253#![feature(placement_in_syntax)]
5bcae85e 254#![feature(question_mark)]
85aaf69f 255#![feature(rand)]
62682a34
SL
256#![feature(raw)]
257#![feature(reflect_marker)]
5bcae85e 258#![feature(repr_simd)]
54a0048b 259#![feature(rustc_attrs)]
9cc50fc6 260#![feature(shared)]
3157f602 261#![feature(sip_hash_13)]
62682a34 262#![feature(slice_bytes)]
92a42be0 263#![feature(slice_concat_ext)]
d9579d0f 264#![feature(slice_patterns)]
85aaf69f 265#![feature(staged_api)]
9cc50fc6 266#![feature(stmt_expr_attributes)]
d9579d0f 267#![feature(str_char)]
62682a34 268#![feature(str_internals)]
92a42be0
SL
269#![feature(str_utf16)]
270#![feature(test, rustc_private)]
271#![feature(thread_local)]
5bcae85e
SL
272#![feature(try_borrow)]
273#![feature(try_from)]
85aaf69f
SL
274#![feature(unboxed_closures)]
275#![feature(unicode)]
c34b1796 276#![feature(unique)]
d9579d0f 277#![feature(unsafe_no_drop_flag, filling_drop)]
e9174d1e 278#![feature(unwind_attributes)]
62682a34 279#![feature(vec_push_all)]
9346a6ac 280#![feature(zero_one)]
5bcae85e 281#![cfg_attr(test, feature(update_panic_count))]
1a4d82fc 282
7453a54e
SL
283// Issue# 30592: Systematically use alloc_system during stage0 since jemalloc
284// might be unavailable or disabled
285#![cfg_attr(stage0, feature(alloc_system))]
286
1a4d82fc
JJ
287// Don't link to std. We are std.
288#![no_std]
289
290#![deny(missing_docs)]
92a42be0 291#![allow(unused_features)] // std may use features in a platform-specific way
7453a54e 292#![cfg_attr(not(stage0), deny(warnings))]
1a4d82fc 293
85aaf69f 294#[cfg(test)] extern crate test;
1a4d82fc 295
e9174d1e
SL
296// We want to reexport a few macros from core but libcore has already been
297// imported by the compiler (via our #[no_std] attribute) In this case we just
298// add a new crate name so we can attach the reexports to it.
85aaf69f 299#[macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq,
92a42be0 300 unreachable, unimplemented, write, writeln, try)]
e9174d1e 301extern crate core as __core;
1a4d82fc
JJ
302
303#[macro_use]
85aaf69f 304#[macro_reexport(vec, format)]
c34b1796 305extern crate collections as core_collections;
1a4d82fc 306
c34b1796 307#[allow(deprecated)] extern crate rand as core_rand;
1a4d82fc 308extern crate alloc;
d9579d0f 309extern crate rustc_unicode;
1a4d82fc
JJ
310extern crate libc;
311
a7813a04
XL
312// We always need an unwinder currently for backtraces
313extern crate unwind;
314
7453a54e
SL
315#[cfg(stage0)]
316extern crate alloc_system;
317
c1a9b12d 318// Make std testable by not duplicating lang items and other globals. See #2912
c34b1796 319#[cfg(test)] extern crate std as realstd;
1a4d82fc
JJ
320
321// NB: These reexports are in the order they should be listed in rustdoc
322
92a42be0 323#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 324pub use core::any;
92a42be0 325#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 326pub use core::cell;
92a42be0 327#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 328pub use core::clone;
92a42be0 329#[stable(feature = "rust1", since = "1.0.0")]
c1a9b12d 330pub use core::cmp;
92a42be0 331#[stable(feature = "rust1", since = "1.0.0")]
c34b1796 332pub use core::convert;
92a42be0 333#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 334pub use core::default;
92a42be0 335#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 336pub use core::hash;
92a42be0 337#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 338pub use core::intrinsics;
92a42be0 339#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 340pub use core::iter;
92a42be0 341#[stable(feature = "rust1", since = "1.0.0")]
c1a9b12d 342pub use core::marker;
92a42be0 343#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 344pub use core::mem;
92a42be0 345#[stable(feature = "rust1", since = "1.0.0")]
c1a9b12d 346pub use core::ops;
92a42be0 347#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 348pub use core::ptr;
92a42be0 349#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 350pub use core::raw;
92a42be0 351#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 352pub use core::result;
92a42be0 353#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 354pub use core::option;
92a42be0 355
c34b1796 356pub mod error;
1a4d82fc 357
92a42be0 358#[stable(feature = "rust1", since = "1.0.0")]
c1a9b12d 359pub use alloc::boxed;
92a42be0 360#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
361pub use alloc::rc;
362
92a42be0 363#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 364pub use core_collections::borrow;
92a42be0 365#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 366pub use core_collections::fmt;
92a42be0 367#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 368pub use core_collections::slice;
92a42be0 369#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 370pub use core_collections::str;
92a42be0 371#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 372pub use core_collections::string;
92a42be0 373#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
374pub use core_collections::vec;
375
92a42be0 376#[stable(feature = "rust1", since = "1.0.0")]
d9579d0f 377pub use rustc_unicode::char;
1a4d82fc
JJ
378
379/* Exported macros */
380
381#[macro_use]
382mod macros;
383
1a4d82fc
JJ
384mod rtdeps;
385
386/* The Prelude. */
387
388pub mod prelude;
389
390
391/* Primitive types */
392
e9174d1e
SL
393// NB: slice and str are primitive types too, but their module docs + primitive
394// doc pages are inlined from the public re-exports of core_collections::{slice,
395// str} above.
1a4d82fc 396
92a42be0 397#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 398pub use core::isize;
92a42be0 399#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 400pub use core::i8;
92a42be0 401#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 402pub use core::i16;
92a42be0 403#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 404pub use core::i32;
92a42be0 405#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 406pub use core::i64;
1a4d82fc 407
92a42be0 408#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 409pub use core::usize;
92a42be0 410#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 411pub use core::u8;
92a42be0 412#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 413pub use core::u16;
92a42be0 414#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 415pub use core::u32;
92a42be0 416#[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 417pub use core::u64;
1a4d82fc
JJ
418
419#[path = "num/f32.rs"] pub mod f32;
420#[path = "num/f64.rs"] pub mod f64;
421
422pub mod ascii;
c34b1796 423
1a4d82fc
JJ
424/* Common traits */
425
1a4d82fc
JJ
426pub mod num;
427
428/* Runtime and platform support */
429
430#[macro_use]
c34b1796 431pub mod thread;
1a4d82fc 432
c34b1796 433pub mod collections;
c34b1796 434pub mod env;
1a4d82fc 435pub mod ffi;
85aaf69f 436pub mod fs;
c34b1796 437pub mod io;
85aaf69f 438pub mod net;
1a4d82fc 439pub mod os;
9cc50fc6 440pub mod panic;
1a4d82fc 441pub mod path;
85aaf69f 442pub mod process;
c34b1796 443pub mod sync;
1a4d82fc 444pub mod time;
9cc50fc6 445mod memchr;
1a4d82fc 446
c34b1796
AL
447#[macro_use]
448#[path = "sys/common/mod.rs"] mod sys_common;
1a4d82fc
JJ
449
450#[cfg(unix)]
451#[path = "sys/unix/mod.rs"] mod sys;
452#[cfg(windows)]
453#[path = "sys/windows/mod.rs"] mod sys;
454
1a4d82fc 455pub mod rt;
85aaf69f 456mod panicking;
9346a6ac
AL
457mod rand;
458
459// Some external utilities of the standard library rely on randomness (aka
460// rustc_back::TempDir and tests) and need a way to get at the OS rng we've got
461// here. This module is not at all intended for stabilization as-is, however,
462// but it may be stabilized long-term. As a result we're exposing a hidden,
463// unstable module so we can get our build working.
464#[doc(hidden)]
e9174d1e 465#[unstable(feature = "rand", issue = "0")]
9346a6ac
AL
466pub mod __rand {
467 pub use rand::{thread_rng, ThreadRng, Rng};
468}
1a4d82fc 469
c1a9b12d
SL
470// Include a number of private modules that exist solely to provide
471// the rustdoc documentation for primitive types. Using `include!`
472// because rustdoc only looks for these modules at the crate level.
473include!("primitive_docs.rs");