]> git.proxmox.com Git - rustc.git/blob - src/libstd/lib.rs
New upstream version 1.20.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 //! # 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/first-edition/deref-coercions.html
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 #![crate_name = "std"]
213 #![stable(feature = "rust1", since = "1.0.0")]
214 #![crate_type = "rlib"]
215 #![crate_type = "dylib"]
216 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
217 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
218 html_root_url = "https://doc.rust-lang.org/nightly/",
219 html_playground_url = "https://play.rust-lang.org/",
220 issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
221 test(no_crate_inject, attr(deny(warnings))),
222 test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))))]
223
224 // Don't link to std. We are std.
225 #![no_std]
226
227 #![deny(missing_docs)]
228 #![deny(missing_debug_implementations)]
229
230 // Tell the compiler to link to either panic_abort or panic_unwind
231 #![needs_panic_runtime]
232
233 // Turn warnings into errors, but only after stage0, where it can be useful for
234 // code to emit warnings during language transitions
235 #![deny(warnings)]
236
237 // std may use features in a platform-specific way
238 #![allow(unused_features)]
239
240 // std is implemented with unstable features, many of which are internal
241 // compiler details that will never be stable
242 #![feature(alloc)]
243 #![feature(allocator_api)]
244 #![feature(alloc_system)]
245 #![feature(allocator_internals)]
246 #![feature(allow_internal_unstable)]
247 #![feature(asm)]
248 #![feature(box_syntax)]
249 #![feature(cfg_target_has_atomic)]
250 #![feature(cfg_target_thread_local)]
251 #![feature(cfg_target_vendor)]
252 #![feature(char_error_internals)]
253 #![feature(char_internals)]
254 #![feature(collections_range)]
255 #![feature(compiler_builtins_lib)]
256 #![feature(const_fn)]
257 #![feature(core_float)]
258 #![feature(core_intrinsics)]
259 #![feature(dropck_eyepatch)]
260 #![feature(exact_size_is_empty)]
261 #![feature(float_from_str_radix)]
262 #![feature(fn_traits)]
263 #![feature(fnbox)]
264 #![feature(fused)]
265 #![feature(generic_param_attrs)]
266 #![feature(hashmap_hasher)]
267 #![feature(heap_api)]
268 #![feature(i128)]
269 #![feature(i128_type)]
270 #![feature(inclusive_range)]
271 #![feature(int_error_internals)]
272 #![feature(integer_atomics)]
273 #![feature(into_cow)]
274 #![feature(lang_items)]
275 #![feature(libc)]
276 #![feature(link_args)]
277 #![feature(linkage)]
278 #![feature(macro_reexport)]
279 #![feature(macro_vis_matcher)]
280 #![feature(needs_panic_runtime)]
281 #![feature(needs_drop)]
282 #![feature(never_type)]
283 #![feature(num_bits_bytes)]
284 #![feature(old_wrapping)]
285 #![feature(on_unimplemented)]
286 #![feature(oom)]
287 #![feature(optin_builtin_traits)]
288 #![feature(panic_unwind)]
289 #![feature(peek)]
290 #![feature(placement_in_syntax)]
291 #![feature(placement_new_protocol)]
292 #![feature(prelude_import)]
293 #![feature(rand)]
294 #![feature(raw)]
295 #![feature(repr_simd)]
296 #![feature(rustc_attrs)]
297 #![feature(shared)]
298 #![feature(sip_hash_13)]
299 #![feature(slice_bytes)]
300 #![feature(slice_concat_ext)]
301 #![feature(slice_patterns)]
302 #![feature(staged_api)]
303 #![feature(stmt_expr_attributes)]
304 #![feature(str_char)]
305 #![feature(str_internals)]
306 #![feature(str_utf16)]
307 #![feature(test, rustc_private)]
308 #![feature(thread_local)]
309 #![feature(toowned_clone_into)]
310 #![feature(try_from)]
311 #![feature(unboxed_closures)]
312 #![feature(unicode)]
313 #![feature(unique)]
314 #![feature(untagged_unions)]
315 #![feature(unwind_attributes)]
316 #![feature(vec_push_all)]
317 #![cfg_attr(test, feature(update_panic_count))]
318
319 #![cfg_attr(not(stage0), default_lib_allocator)]
320 #![cfg_attr(stage0, feature(associated_consts))]
321
322 // Explicitly import the prelude. The compiler uses this same unstable attribute
323 // to import the prelude implicitly when building crates that depend on std.
324 #[prelude_import]
325 #[allow(unused)]
326 use prelude::v1::*;
327
328 // Access to Bencher, etc.
329 #[cfg(test)] extern crate test;
330
331 // We want to reexport a few macros from core but libcore has already been
332 // imported by the compiler (via our #[no_std] attribute) In this case we just
333 // add a new crate name so we can attach the reexports to it.
334 #[macro_reexport(assert, assert_eq, assert_ne, debug_assert, debug_assert_eq,
335 debug_assert_ne, unreachable, unimplemented, write, writeln, try)]
336 extern crate core as __core;
337
338 #[allow(deprecated)] extern crate rand as core_rand;
339 #[macro_use]
340 #[macro_reexport(vec, format)]
341 extern crate alloc;
342 extern crate alloc_system;
343 extern crate std_unicode;
344 extern crate libc;
345
346 // We always need an unwinder currently for backtraces
347 extern crate unwind;
348
349 // compiler-rt intrinsics
350 extern crate compiler_builtins;
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 // wolud 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)] extern crate std as realstd;
359
360 // The standard macros that are not built-in to the compiler.
361 #[macro_use]
362 mod macros;
363
364 // The Rust prelude
365 pub mod prelude;
366
367 // Public module declarations and reexports
368 #[stable(feature = "rust1", since = "1.0.0")]
369 pub use core::any;
370 #[stable(feature = "rust1", since = "1.0.0")]
371 pub use core::cell;
372 #[stable(feature = "rust1", since = "1.0.0")]
373 pub use core::clone;
374 #[stable(feature = "rust1", since = "1.0.0")]
375 pub use core::cmp;
376 #[stable(feature = "rust1", since = "1.0.0")]
377 pub use core::convert;
378 #[stable(feature = "rust1", since = "1.0.0")]
379 pub use core::default;
380 #[stable(feature = "rust1", since = "1.0.0")]
381 pub use core::hash;
382 #[stable(feature = "rust1", since = "1.0.0")]
383 pub use core::intrinsics;
384 #[stable(feature = "rust1", since = "1.0.0")]
385 pub use core::iter;
386 #[stable(feature = "rust1", since = "1.0.0")]
387 pub use core::marker;
388 #[stable(feature = "rust1", since = "1.0.0")]
389 pub use core::mem;
390 #[stable(feature = "rust1", since = "1.0.0")]
391 pub use core::ops;
392 #[stable(feature = "rust1", since = "1.0.0")]
393 pub use core::ptr;
394 #[stable(feature = "rust1", since = "1.0.0")]
395 pub use core::raw;
396 #[stable(feature = "rust1", since = "1.0.0")]
397 pub use core::result;
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub use core::option;
400 #[stable(feature = "rust1", since = "1.0.0")]
401 pub use core::isize;
402 #[stable(feature = "rust1", since = "1.0.0")]
403 pub use core::i8;
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub use core::i16;
406 #[stable(feature = "rust1", since = "1.0.0")]
407 pub use core::i32;
408 #[stable(feature = "rust1", since = "1.0.0")]
409 pub use core::i64;
410 #[unstable(feature = "i128", issue = "35118")]
411 pub use core::i128;
412 #[stable(feature = "rust1", since = "1.0.0")]
413 pub use core::usize;
414 #[stable(feature = "rust1", since = "1.0.0")]
415 pub use core::u8;
416 #[stable(feature = "rust1", since = "1.0.0")]
417 pub use core::u16;
418 #[stable(feature = "rust1", since = "1.0.0")]
419 pub use core::u32;
420 #[stable(feature = "rust1", since = "1.0.0")]
421 pub use core::u64;
422 #[stable(feature = "rust1", since = "1.0.0")]
423 pub use alloc::boxed;
424 #[stable(feature = "rust1", since = "1.0.0")]
425 pub use alloc::rc;
426 #[stable(feature = "rust1", since = "1.0.0")]
427 pub use alloc::borrow;
428 #[stable(feature = "rust1", since = "1.0.0")]
429 pub use alloc::fmt;
430 #[stable(feature = "rust1", since = "1.0.0")]
431 pub use alloc::slice;
432 #[stable(feature = "rust1", since = "1.0.0")]
433 pub use alloc::str;
434 #[stable(feature = "rust1", since = "1.0.0")]
435 pub use alloc::string;
436 #[stable(feature = "rust1", since = "1.0.0")]
437 pub use alloc::vec;
438 #[stable(feature = "rust1", since = "1.0.0")]
439 pub use std_unicode::char;
440 #[unstable(feature = "i128", issue = "35118")]
441 pub use core::u128;
442
443 pub mod f32;
444 pub mod f64;
445
446 #[macro_use]
447 pub mod thread;
448 pub mod ascii;
449 pub mod collections;
450 pub mod env;
451 pub mod error;
452 pub mod ffi;
453 pub mod fs;
454 pub mod io;
455 pub mod net;
456 pub mod num;
457 pub mod os;
458 pub mod panic;
459 pub mod path;
460 pub mod process;
461 pub mod sync;
462 pub mod time;
463 pub mod heap;
464
465 // Platform-abstraction modules
466 #[macro_use]
467 mod sys_common;
468 mod sys;
469
470 // Private support modules
471 mod panicking;
472 mod rand;
473 mod memchr;
474
475 // The runtime entry point and a few unstable public functions used by the
476 // compiler
477 pub mod rt;
478
479 // Some external utilities of the standard library rely on randomness (aka
480 // rustc_back::TempDir and tests) and need a way to get at the OS rng we've got
481 // here. This module is not at all intended for stabilization as-is, however,
482 // but it may be stabilized long-term. As a result we're exposing a hidden,
483 // unstable module so we can get our build working.
484 #[doc(hidden)]
485 #[unstable(feature = "rand", issue = "27703")]
486 pub mod __rand {
487 pub use rand::{thread_rng, ThreadRng, Rng};
488 }
489
490 // Include a number of private modules that exist solely to provide
491 // the rustdoc documentation for primitive types. Using `include!`
492 // because rustdoc only looks for these modules at the crate level.
493 include!("primitive_docs.rs");