]> git.proxmox.com Git - rustc.git/blob - src/libstd/lib.rs
New upstream version 1.15.0+dfsg1
[rustc.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].
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 //! # A Tour of The Rust Standard Library
98 //!
99 //! The rest of this crate documentation is dedicated to pointing out notable
100 //! features of The Rust Standard Library.
101 //!
102 //! ## Containers and collections
103 //!
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.
108 //!
109 //! The standard library exposes three common ways to deal with contiguous
110 //! regions of memory:
111 //!
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.
116 //!
117 //! Slices can only be handled through some kind of *pointer*, and as such come
118 //! in many flavors such as:
119 //!
120 //! * `&[T]` - *shared slice*
121 //! * `&mut [T]` - *mutable slice*
122 //! * [`Box<[T]>`][owned slice] - *owned slice*
123 //!
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.
128 //!
129 //! For converting to strings use the [`format!`] macro, and for converting from
130 //! strings use the [`FromStr`] trait.
131 //!
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.
137 //!
138 //! The [`collections`] module defines maps, sets, linked lists and other
139 //! typical collection types, including the common [`HashMap<K, V>`].
140 //!
141 //! ## Platform abstractions and I/O
142 //!
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
155 //! [`MIN`]: i32/constant.MIN.html
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
193 //! [crate root]: ../book/crates-and-modules.html#basic-terminology-crates-and-modules
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
200
201 #![crate_name = "std"]
202 #![stable(feature = "rust1", since = "1.0.0")]
203 #![crate_type = "rlib"]
204 #![crate_type = "dylib"]
205 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
206 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
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/",
210 test(no_crate_inject, attr(deny(warnings))),
211 test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))))]
212
213 // Don't link to std. We are std.
214 #![no_std]
215
216 #![deny(missing_docs)]
217
218 // Tell the compiler to link to either panic_abort or panic_unwind
219 #![needs_panic_runtime]
220
221 // Always use alloc_system during stage0 since jemalloc might be unavailable or
222 // disabled (Issue #30592)
223 #![cfg_attr(stage0, feature(alloc_system))]
224
225 // Turn warnings into errors, but only after stage0, where it can be useful for
226 // code to emit warnings during language transitions
227 #![cfg_attr(not(stage0), deny(warnings))]
228
229 // std may use features in a platform-specific way
230 #![allow(unused_features)]
231
232 // std is implemented with unstable features, many of which are internal
233 // compiler details that will never be stable
234 #![feature(alloc)]
235 #![feature(allow_internal_unstable)]
236 #![feature(asm)]
237 #![feature(associated_consts)]
238 #![feature(borrow_state)]
239 #![feature(box_syntax)]
240 #![feature(cfg_target_has_atomic)]
241 #![feature(cfg_target_thread_local)]
242 #![feature(cfg_target_vendor)]
243 #![feature(char_escape_debug)]
244 #![feature(char_internals)]
245 #![feature(collections)]
246 #![feature(collections_bound)]
247 #![feature(collections_range)]
248 #![feature(compiler_builtins_lib)]
249 #![feature(const_fn)]
250 #![feature(core_float)]
251 #![feature(core_intrinsics)]
252 #![feature(dropck_parametricity)]
253 #![feature(exact_size_is_empty)]
254 #![feature(float_extras)]
255 #![feature(float_from_str_radix)]
256 #![feature(fn_traits)]
257 #![feature(fnbox)]
258 #![feature(fused)]
259 #![feature(hashmap_hasher)]
260 #![feature(heap_api)]
261 #![feature(inclusive_range)]
262 #![feature(int_error_internals)]
263 #![feature(integer_atomics)]
264 #![feature(into_cow)]
265 #![feature(lang_items)]
266 #![feature(libc)]
267 #![feature(link_args)]
268 #![feature(linkage)]
269 #![feature(macro_reexport)]
270 #![feature(needs_panic_runtime)]
271 #![feature(num_bits_bytes)]
272 #![feature(old_wrapping)]
273 #![feature(on_unimplemented)]
274 #![feature(oom)]
275 #![feature(optin_builtin_traits)]
276 #![feature(panic_unwind)]
277 #![feature(placement_in_syntax)]
278 #![feature(prelude_import)]
279 #![feature(rand)]
280 #![feature(raw)]
281 #![feature(repr_simd)]
282 #![feature(rustc_attrs)]
283 #![feature(shared)]
284 #![feature(sip_hash_13)]
285 #![feature(slice_bytes)]
286 #![feature(slice_concat_ext)]
287 #![feature(slice_patterns)]
288 #![feature(staged_api)]
289 #![feature(stmt_expr_attributes)]
290 #![feature(str_char)]
291 #![feature(str_internals)]
292 #![feature(str_utf16)]
293 #![feature(test, rustc_private)]
294 #![feature(thread_local)]
295 #![feature(try_from)]
296 #![feature(unboxed_closures)]
297 #![feature(unicode)]
298 #![feature(unique)]
299 #![feature(unwind_attributes)]
300 #![feature(vec_push_all)]
301 #![feature(zero_one)]
302 #![cfg_attr(test, feature(update_panic_count))]
303
304 // Explicitly import the prelude. The compiler uses this same unstable attribute
305 // to import the prelude implicitly when building crates that depend on std.
306 #[prelude_import]
307 #[allow(unused)]
308 use prelude::v1::*;
309
310 // Access to Bencher, etc.
311 #[cfg(test)] extern crate test;
312
313 // We want to reexport a few macros from core but libcore has already been
314 // imported by the compiler (via our #[no_std] attribute) In this case we just
315 // add a new crate name so we can attach the reexports to it.
316 #[macro_reexport(assert, assert_eq, assert_ne, debug_assert, debug_assert_eq,
317 debug_assert_ne, unreachable, unimplemented, write, writeln, try)]
318 extern crate core as __core;
319
320 #[macro_use]
321 #[macro_reexport(vec, format)]
322 extern crate collections as core_collections;
323
324 #[allow(deprecated)] extern crate rand as core_rand;
325 extern crate alloc;
326 extern crate std_unicode;
327 extern crate libc;
328
329 // We always need an unwinder currently for backtraces
330 extern crate unwind;
331
332 #[cfg(stage0)]
333 extern crate alloc_system;
334
335 // compiler-rt intrinsics
336 extern crate compiler_builtins;
337
338 // During testing, this crate is not actually the "real" std library, but rather
339 // it links to the real std library, which was compiled from this same source
340 // code. So any lang items std defines are conditionally excluded (or else they
341 // wolud generate duplicate lang item errors), and any globals it defines are
342 // _not_ the globals used by "real" std. So this import, defined only during
343 // testing gives test-std access to real-std lang items and globals. See #2912
344 #[cfg(test)] extern crate std as realstd;
345
346 // The standard macros that are not built-in to the compiler.
347 #[macro_use]
348 mod macros;
349
350 // The Rust prelude
351 pub mod prelude;
352
353 // Public module declarations and reexports
354 #[stable(feature = "rust1", since = "1.0.0")]
355 pub use core::any;
356 #[stable(feature = "rust1", since = "1.0.0")]
357 pub use core::cell;
358 #[stable(feature = "rust1", since = "1.0.0")]
359 pub use core::clone;
360 #[stable(feature = "rust1", since = "1.0.0")]
361 pub use core::cmp;
362 #[stable(feature = "rust1", since = "1.0.0")]
363 pub use core::convert;
364 #[stable(feature = "rust1", since = "1.0.0")]
365 pub use core::default;
366 #[stable(feature = "rust1", since = "1.0.0")]
367 pub use core::hash;
368 #[stable(feature = "rust1", since = "1.0.0")]
369 pub use core::intrinsics;
370 #[stable(feature = "rust1", since = "1.0.0")]
371 pub use core::iter;
372 #[stable(feature = "rust1", since = "1.0.0")]
373 pub use core::marker;
374 #[stable(feature = "rust1", since = "1.0.0")]
375 pub use core::mem;
376 #[stable(feature = "rust1", since = "1.0.0")]
377 pub use core::ops;
378 #[stable(feature = "rust1", since = "1.0.0")]
379 pub use core::ptr;
380 #[stable(feature = "rust1", since = "1.0.0")]
381 pub use core::raw;
382 #[stable(feature = "rust1", since = "1.0.0")]
383 pub use core::result;
384 #[stable(feature = "rust1", since = "1.0.0")]
385 pub use core::option;
386 #[stable(feature = "rust1", since = "1.0.0")]
387 pub use core::isize;
388 #[stable(feature = "rust1", since = "1.0.0")]
389 pub use core::i8;
390 #[stable(feature = "rust1", since = "1.0.0")]
391 pub use core::i16;
392 #[stable(feature = "rust1", since = "1.0.0")]
393 pub use core::i32;
394 #[stable(feature = "rust1", since = "1.0.0")]
395 pub use core::i64;
396 #[stable(feature = "rust1", since = "1.0.0")]
397 pub use core::usize;
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub use core::u8;
400 #[stable(feature = "rust1", since = "1.0.0")]
401 pub use core::u16;
402 #[stable(feature = "rust1", since = "1.0.0")]
403 pub use core::u32;
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub use core::u64;
406 #[stable(feature = "rust1", since = "1.0.0")]
407 pub use alloc::boxed;
408 #[stable(feature = "rust1", since = "1.0.0")]
409 pub use alloc::rc;
410 #[stable(feature = "rust1", since = "1.0.0")]
411 pub use core_collections::borrow;
412 #[stable(feature = "rust1", since = "1.0.0")]
413 pub use core_collections::fmt;
414 #[stable(feature = "rust1", since = "1.0.0")]
415 pub use core_collections::slice;
416 #[stable(feature = "rust1", since = "1.0.0")]
417 pub use core_collections::str;
418 #[stable(feature = "rust1", since = "1.0.0")]
419 pub use core_collections::string;
420 #[stable(feature = "rust1", since = "1.0.0")]
421 pub use core_collections::vec;
422 #[stable(feature = "rust1", since = "1.0.0")]
423 pub use std_unicode::char;
424
425 pub mod f32;
426 pub mod f64;
427
428 #[macro_use]
429 pub mod thread;
430 pub mod ascii;
431 pub mod collections;
432 pub mod env;
433 pub mod error;
434 pub mod ffi;
435 pub mod fs;
436 pub mod io;
437 pub mod net;
438 pub mod num;
439 pub mod os;
440 pub mod panic;
441 pub mod path;
442 pub mod process;
443 pub mod sync;
444 pub mod time;
445
446 // Platform-abstraction modules
447 #[macro_use]
448 mod sys_common;
449 mod sys;
450
451 // Private support modules
452 mod panicking;
453 mod rand;
454 mod memchr;
455
456 // This module just defines per-platform native library dependencies
457 mod rtdeps;
458
459 // The runtime entry point and a few unstable public functions used by the
460 // compiler
461 pub mod rt;
462
463 // Some external utilities of the standard library rely on randomness (aka
464 // rustc_back::TempDir and tests) and need a way to get at the OS rng we've got
465 // here. This module is not at all intended for stabilization as-is, however,
466 // but it may be stabilized long-term. As a result we're exposing a hidden,
467 // unstable module so we can get our build working.
468 #[doc(hidden)]
469 #[unstable(feature = "rand", issue = "0")]
470 pub mod __rand {
471 pub use rand::{thread_rng, ThreadRng, Rng};
472 }
473
474 // Include a number of private modules that exist solely to provide
475 // the rustdoc documentation for primitive types. Using `include!`
476 // because rustdoc only looks for these modules at the crate level.
477 include!("primitive_docs.rs");