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