]> git.lizzy.rs Git - rust.git/blob - src/libstd/lib.rs
fca4c66112eb6c89e3ad878ef0f442abbb6cf1e2
[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
14 //! software, a set of minimal and battle-tested shared abstractions
15 //! for the [broader Rust ecosystem](https://crates.io). It offers
16 //! core types, like [`Vec`](vec/index.html)
17 //! and [`Option`](option/index.html), library-defined [operations on
18 //! language primitives](#primitives), [standard macros](#macros),
19 //! [I/O](io/index.html) and [multithreading](thread/index.html), among
20 //! [many other
21 //! things](#what-is-in-the-standard-library-documentation?).
22 //!
23 //! `std` is available to all Rust crates by default, just as if each
24 //! one contained an `extern crate std` import at the [crate
25 //! root][book-crate-root]. Therefore the standard library can be
26 //! accessed in [`use`][book-use] statements through the path `std`,
27 //! as in [`use std::env`](env/index.html), or in expressions
28 //! through the absolute path `::std`, as in
29 //! [`::std::env::args()`](env/fn.args.html).
30 //!
31 //! [book-crate-root]: ../book/crates-and-modules.html#basic-terminology:-crates-and-modules
32 //! [book-use]: ../book/crates-and-modules.html#importing-modules-with-use
33 //!
34 //! # How to read this documentation
35 //!
36 //! If you already know the name of what you are looking for the
37 //! fastest way to find it is to use the <a href="#"
38 //! onclick="focusSearchBar();">search bar</a> at the top of the page.
39 //!
40 //! Otherwise, you may want to jump to one of these useful sections:
41 //!
42 //! * [`std::*` modules](#modules)
43 //! * [Primitive types](#primitives)
44 //! * [Standard macros](#macros)
45 //! * [The Rust Prelude](prelude/index.html)
46 //!
47 //! If this is your first time, the documentation for the standard
48 //! library is written to be casually perused. Clicking on interesting
49 //! things should generally lead you to interesting places. Still,
50 //! there are important bits you don't want to miss, so read on for a
51 //! tour of the standard library and its documentation!
52 //!
53 //! Once you are familiar with the contents of the standard library
54 //! you may begin to find the verbosity of the prose distracting. At
55 //! this stage in your development you may want to press the **[-]**
56 //! button near the top of the page to collapse it into a more
57 //! skimmable view.
58 //!
59 //! While you are looking at that **[-]** button also notice the
60 //! **[src]** button. Rust's API documentation comes with the source
61 //! code and you are encouraged to read it. The standard library
62 //! source is generally high quality and a peek behind the curtains is
63 //! often enlightening.
64 //!
65 //! # What is in the standard library documentation?
66 //!
67 //! First of all, The Rust Standard Library is divided into a number
68 //! of focused modules, [all listed further down this page](#modules).
69 //! These modules are the bedrock upon which all of Rust is forged,
70 //! and they have mighty names like [`std::slice`](slice/index.html)
71 //! and [`std::cmp`](cmp/index.html). Modules' documentation typically
72 //! includes an overview of the module along with examples, and are
73 //! a smart place to start familiarizing yourself with the library.
74 //!
75 //! Second, implicit methods on [primitive
76 //! types](../book/primitive-types.html) are documented here. This can
77 //! be a source of confusion for two reasons:
78 //!
79 //! 1. While primitives are implemented by the compiler, the standard
80 //!    library implements methods directly on the primitive types (and
81 //!    it is the only library that does so), which are [documented in
82 //!    the section on primitives](#primitives).
83 //! 2. The standard library exports many modules *with the same name
84 //!    as primitive types*. These define additional items related
85 //!    to the primitive type, but not the all-important methods.
86 //!
87 //! So for example there is a [page for the primitive type
88 //! `i32`](primitive.i32.html) that lists all the methods that can be
89 //! called on 32-bit integers (very useful), and there is a [page for
90 //! the module `std::i32`](i32/index.html) that documents the constant
91 //! values `MIN` and `MAX` (rarely useful).
92 //!
93 //! Note the documentation for the primitives
94 //! [`str`](primitive.str.html) and [`[T]`](primitive.slice.html)
95 //! (also called 'slice'). Many method calls on
96 //! [`String`](string/struct.String.html) and
97 //! [`Vec`](vec/struct.Vec.html) are actually calls to methods on
98 //! `str` and `[T]` respectively, via [deref
99 //! coercions](../book/deref-coercions.html).
100 //!
101 //! Third, the standard library defines [The Rust
102 //! Prelude](prelude/index.html), a small collection of items - mostly
103 //! traits - that are imported into every module of every crate. The
104 //! traits in the prelude are pervasive, making the prelude
105 //! documentation a good entry point to learning about the library.
106 //!
107 //! And finally, the standard library exports a number of standard
108 //! macros, and [lists them on this page](#macros) (technically, not
109 //! all of the standard macros are defined by the standard library -
110 //! some are defined by the compiler - but they are documented here
111 //! the same). Like the prelude, the standard macros are imported by
112 //! default into all crates.
113 //!
114 //! # A Tour of The Rust Standard Library
115 //!
116 //! The rest of this crate documentation is dedicated to pointing
117 //! out notable features of The Rust Standard Library.
118 //!
119 //! ## Containers and collections
120 //!
121 //! The [`option`](option/index.html) and
122 //! [`result`](result/index.html) modules define optional and
123 //! error-handling types, `Option` and `Result`. The
124 //! [`iter`](iter/index.html) module defines Rust's iterator trait,
125 //! [`Iterator`](iter/trait.Iterator.html), which works with the `for`
126 //! loop to access collections.
127 //!
128 //! The standard library exposes 3 common ways to deal with contiguous
129 //! regions of memory:
130 //!
131 //! * [`Vec<T>`](vec/index.html) - A heap-allocated *vector* that is
132 //! resizable at runtime.
133 //! * [`[T; n]`](primitive.array.html) - An inline *array* with a
134 //! fixed size at compile time.
135 //! * [`[T]`](primitive.slice.html) - A dynamically sized *slice* into
136 //! any other kind of contiguous storage, whether heap-allocated or
137 //! not.
138 //!
139 //! Slices can only be handled through some kind of *pointer*, and as
140 //! such come in many flavours such as:
141 //!
142 //! * `&[T]` - *shared slice*
143 //! * `&mut [T]` - *mutable slice*
144 //! * [`Box<[T]>`](boxed/index.html) - *owned slice*
145 //!
146 //! `str`, a UTF-8 string slice, is a primitive type, and the standard
147 //! library defines [many methods for it](primitive.str.html). Rust
148 //! `str`s are typically accessed as immutable references: `&str`. Use
149 //! the owned `String` type defined in [`string`](string/index.html)
150 //! for building and mutating strings.
151 //!
152 //! For converting to strings use the [`format!`](fmt/index.html)
153 //! macro, and for converting from strings use the
154 //! [`FromStr`](str/trait.FromStr.html) trait.
155 //!
156 //! Data may be shared by placing it in a reference-counted box or the
157 //! [`Rc`](rc/index.html) type, and if further contained in a [`Cell`
158 //! or `RefCell`](cell/index.html), may be mutated as well as shared.
159 //! Likewise, in a concurrent setting it is common to pair an
160 //! atomically-reference-counted box, [`Arc`](sync/struct.Arc.html),
161 //! with a [`Mutex`](sync/struct.Mutex.html) to get the same effect.
162 //!
163 //! The [`collections`](collections/index.html) module defines maps,
164 //! sets, linked lists and other typical collection types, including
165 //! the common [`HashMap`](collections/struct.HashMap.html).
166 //!
167 //! ## Platform abstractions and I/O
168 //!
169 //! Besides basic data types, the standard library is largely concerned
170 //! with abstracting over differences in common platforms, most notably
171 //! Windows and Unix derivatives.
172 //!
173 //! Common types of I/O, including [files](fs/struct.File.html),
174 //! [TCP](net/struct.TcpStream.html),
175 //! [UDP](net/struct.UdpSocket.html), are defined in the
176 //! [`io`](io/index.html), [`fs`](fs/index.html), and
177 //! [`net`](net/index.html) modules.
178 //!
179 //! The [`thread`](thread/index.html) module contains Rust's threading
180 //! abstractions. [`sync`](sync/index.html) contains further
181 //! primitive shared memory types, including
182 //! [`atomic`](sync/atomic/index.html) and
183 //! [`mpsc`](sync/mpsc/index.html), which contains the channel types
184 //! for message passing.
185 //!
186
187 // Do not remove on snapshot creation. Needed for bootstrap. (Issue #22364)
188 #![cfg_attr(stage0, feature(custom_attribute))]
189 #![crate_name = "std"]
190 #![stable(feature = "rust1", since = "1.0.0")]
191 #![staged_api]
192 #![crate_type = "rlib"]
193 #![crate_type = "dylib"]
194 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
195        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
196        html_root_url = "https://doc.rust-lang.org/nightly/",
197        html_playground_url = "https://play.rust-lang.org/",
198        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
199        test(no_crate_inject, attr(deny(warnings))),
200        test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))))]
201
202 #![feature(alloc)]
203 #![feature(allow_internal_unstable)]
204 #![feature(associated_consts)]
205 #![feature(borrow_state)]
206 #![feature(box_raw)]
207 #![feature(box_syntax)]
208 #![feature(char_from_unchecked)]
209 #![feature(char_internals)]
210 #![feature(clone_from_slice)]
211 #![feature(collections)]
212 #![feature(collections_bound)]
213 #![feature(const_fn)]
214 #![feature(core)]
215 #![feature(core_float)]
216 #![feature(core_intrinsics)]
217 #![feature(core_simd)]
218 #![feature(drain)]
219 #![feature(fnbox)]
220 #![feature(heap_api)]
221 #![feature(int_error_internals)]
222 #![feature(into_cow)]
223 #![feature(iter_order)]
224 #![feature(lang_items)]
225 #![feature(libc)]
226 #![feature(linkage, thread_local, asm)]
227 #![feature(macro_reexport)]
228 #![feature(slice_concat_ext)]
229 #![feature(no_std)]
230 #![feature(oom)]
231 #![feature(optin_builtin_traits)]
232 #![feature(placement_in_syntax)]
233 #![feature(rand)]
234 #![feature(raw)]
235 #![feature(reflect_marker)]
236 #![feature(slice_bytes)]
237 #![feature(slice_patterns)]
238 #![feature(staged_api)]
239 #![feature(str_char)]
240 #![feature(str_internals)]
241 #![feature(unboxed_closures)]
242 #![feature(unicode)]
243 #![feature(unique)]
244 #![feature(unsafe_no_drop_flag, filling_drop)]
245 #![feature(decode_utf16)]
246 #![feature(vec_push_all)]
247 #![feature(vec_resize)]
248 #![feature(wrapping)]
249 #![feature(zero_one)]
250 #![cfg_attr(windows, feature(str_utf16))]
251 #![cfg_attr(test, feature(float_from_str_radix, range_inclusive, float_extras, hash_default))]
252 #![cfg_attr(test, feature(test, rustc_private, float_consts))]
253 #![cfg_attr(target_env = "msvc", feature(link_args))]
254
255 // Don't link to std. We are std.
256 #![no_std]
257
258 #![deny(missing_docs)]
259
260 #[cfg(test)] extern crate test;
261 #[cfg(test)] #[macro_use] extern crate log;
262
263 // We want to reexport a few macros from core but libcore has already been
264 // imported by the compiler (via our #[no_std] attribute) In this case we just
265 // add a new crate name so we can attach the reexports to it.
266 #[macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq,
267                  unreachable, unimplemented, write, writeln)]
268 extern crate core as __core;
269
270 #[macro_use]
271 #[macro_reexport(vec, format)]
272 extern crate collections as core_collections;
273
274 #[allow(deprecated)] extern crate rand as core_rand;
275 extern crate alloc;
276 extern crate rustc_unicode;
277 extern crate libc;
278
279 // Make std testable by not duplicating lang items and other globals. See #2912
280 #[cfg(test)] extern crate std as realstd;
281
282 // NB: These reexports are in the order they should be listed in rustdoc
283
284 pub use core::any;
285 pub use core::cell;
286 pub use core::clone;
287 pub use core::cmp;
288 pub use core::convert;
289 pub use core::default;
290 pub use core::hash;
291 pub use core::intrinsics;
292 pub use core::iter;
293 pub use core::marker;
294 pub use core::mem;
295 pub use core::ops;
296 pub use core::ptr;
297 pub use core::raw;
298 #[allow(deprecated)]
299 pub use core::simd;
300 pub use core::result;
301 pub use core::option;
302 pub mod error;
303
304 pub use alloc::boxed;
305 pub use alloc::rc;
306
307 pub use core_collections::borrow;
308 pub use core_collections::fmt;
309 pub use core_collections::slice;
310 pub use core_collections::str;
311 pub use core_collections::string;
312 pub use core_collections::vec;
313
314 pub use rustc_unicode::char;
315
316 /* Exported macros */
317
318 #[macro_use]
319 mod macros;
320
321 mod rtdeps;
322
323 /* The Prelude. */
324
325 pub mod prelude;
326
327
328 /* Primitive types */
329
330 // NB: slice and str are primitive types too, but their module docs + primitive
331 // doc pages are inlined from the public re-exports of core_collections::{slice,
332 // str} above.
333
334 pub use core::isize;
335 pub use core::i8;
336 pub use core::i16;
337 pub use core::i32;
338 pub use core::i64;
339
340 pub use core::usize;
341 pub use core::u8;
342 pub use core::u16;
343 pub use core::u32;
344 pub use core::u64;
345
346 #[path = "num/f32.rs"]   pub mod f32;
347 #[path = "num/f64.rs"]   pub mod f64;
348
349 pub mod ascii;
350
351 /* Common traits */
352
353 pub mod num;
354
355 /* Runtime and platform support */
356
357 #[macro_use]
358 pub mod thread;
359
360 pub mod collections;
361 pub mod dynamic_lib;
362 pub mod env;
363 pub mod ffi;
364 pub mod fs;
365 pub mod io;
366 pub mod net;
367 pub mod os;
368 pub mod path;
369 pub mod process;
370 pub mod sync;
371 pub mod time;
372
373 #[macro_use]
374 #[path = "sys/common/mod.rs"] mod sys_common;
375
376 #[cfg(unix)]
377 #[path = "sys/unix/mod.rs"] mod sys;
378 #[cfg(windows)]
379 #[path = "sys/windows/mod.rs"] mod sys;
380
381 pub mod rt;
382 mod panicking;
383 mod rand;
384
385 // Some external utilities of the standard library rely on randomness (aka
386 // rustc_back::TempDir and tests) and need a way to get at the OS rng we've got
387 // here. This module is not at all intended for stabilization as-is, however,
388 // but it may be stabilized long-term. As a result we're exposing a hidden,
389 // unstable module so we can get our build working.
390 #[doc(hidden)]
391 #[unstable(feature = "rand", issue = "0")]
392 pub mod __rand {
393     pub use rand::{thread_rng, ThreadRng, Rng};
394 }
395
396 // Include a number of private modules that exist solely to provide
397 // the rustdoc documentation for primitive types. Using `include!`
398 // because rustdoc only looks for these modules at the crate level.
399 include!("primitive_docs.rs");