Skip to content

Commit 3a5bc73

Browse files
committed
Rollup merge of rust-lang#26977 - brson:stddoc, r=Gankro
Yet another attempt to make the prose on the std crate page clearer and more informative. This does a lot of things: tightens up the opening, adds useful links (including a link to the search bar), offers guidance on how to use the docs, and expands the prelude docs as a useful newbie entrypoint. r? @steveklabnik cc @aturon
2 parents 9ac3f42 + 2881bba commit 3a5bc73

File tree

3 files changed

+229
-35
lines changed

3 files changed

+229
-35
lines changed

src/librustdoc/html/static/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,8 @@
951951
}());
952952

953953
}());
954+
955+
// Sets the focus on the search bar at the top of the page
956+
function focusSearchBar() {
957+
document.getElementsByName('search')[0].focus();
958+
}

src/libstd/lib.rs

Lines changed: 128 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,116 @@
1010

1111
//! # The Rust Standard Library
1212
//!
13-
//! The Rust Standard Library provides the essential runtime
14-
//! functionality for building portable Rust software.
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?).
1523
//!
16-
//! The Rust Standard Library is available to all Rust crates by
17-
//! default, just as if contained an `extern crate std` import at the
18-
//! crate root. Therefore the standard library can be accessed in
19-
//! `use` statements through the path `std`, as in `use std::thread`,
20-
//! or in expressions through the absolute path `::std`, as in
21-
//! `::std::thread::sleep_ms(100)`.
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).
2231
//!
23-
//! Furthermore, the standard library defines [The Rust
24-
//! Prelude](prelude/index.html), a small collection of items, mostly
25-
//! traits, that are imported into and available in every module.
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
2634
//!
27-
//! ## What is in the standard library
35+
//! # How to read this documentation
2836
//!
29-
//! The standard library is a set of minimal, battle-tested
30-
//! core types and shared abstractions for the [broader Rust
31-
//! ecosystem](https://crates.io) to build on.
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.
3240
//!
33-
//! The [primitive types](#primitives), though not defined in the
34-
//! standard library, are documented here, as are the predefined
35-
//! [macros](#macros).
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.
36123
//!
37124
//! ## Containers and collections
38125
//!
@@ -43,17 +130,29 @@
43130
//! [`Iterator`](iter/trait.Iterator.html), which works with the `for`
44131
//! loop to access collections.
45132
//!
46-
//! The common container type, `Vec`, a growable vector backed by an array,
47-
//! lives in the [`vec`](vec/index.html) module. Contiguous, unsized regions
48-
//! of memory, `[T]`, commonly called "slices", and their borrowed versions,
49-
//! `&[T]`, commonly called "borrowed slices", are built-in types for which the
50-
//! [`slice`](slice/index.html) module defines many methods.
133+
//! The standard library exposes 3 common ways to deal with contiguous
134+
//! regions of memory:
51135
//!
52-
//! `&str`, a UTF-8 string, is a built-in type, and the standard library
53-
//! defines methods for it on a variety of traits in the
54-
//! [`str`](str/index.html) module. Rust strings are immutable;
55-
//! use the `String` type defined in [`string`](string/index.html)
56-
//! for a mutable string builder.
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.
57156
//!
58157
//! For converting to strings use the [`format!`](fmt/index.html)
59158
//! macro, and for converting from strings use the
@@ -88,6 +187,7 @@
88187
//! [`atomic`](sync/atomic/index.html) and
89188
//! [`mpsc`](sync/mpsc/index.html), which contains the channel types
90189
//! for message passing.
190+
//!
91191
92192
// Do not remove on snapshot creation. Needed for bootstrap. (Issue #22364)
93193
#![cfg_attr(stage0, feature(custom_attribute))]

src/libstd/prelude/mod.rs

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,107 @@
2222
//! with the `std::` path prefix, as in `use std::vec`, `use std::thread::spawn`,
2323
//! etc.
2424
//!
25-
//! Additionally, `std` contains a `prelude` module that reexports many of the
26-
//! most common traits, types and functions. The contents of the prelude are
27-
//! imported into every *module* by default. Implicitly, all modules behave as if
28-
//! they contained the following prologue:
25+
//! Additionally, `std` contains a versioned *prelude* that reexports many of the
26+
//! most common traits, types and functions. *The contents of the prelude are
27+
//! imported into every module by default*. Implicitly, all modules behave as if
28+
//! they contained the following [`use` statement][book-use]:
29+
//!
30+
//! [book-use]: ../../book/crates-and-modules.html#importing-modules-with-use
2931
//!
3032
//! ```ignore
3133
//! use std::prelude::v1::*;
3234
//! ```
3335
//!
34-
//! The prelude is primarily concerned with exporting *traits* that are so
35-
//! pervasive that it would be obnoxious to import for every use, particularly
36-
//! those that define methods on primitive types.
36+
//! The prelude is primarily concerned with exporting *traits* that
37+
//! are so pervasive that they would be onerous to import for every use,
38+
//! particularly those that are commonly mentioned in [generic type
39+
//! bounds][book-traits].
40+
//!
41+
//! The current version of the prelude (version 1) lives in
42+
//! [`std::prelude::v1`](v1/index.html), and reexports the following.
43+
//!
44+
//! * `std::marker::`{
45+
//! [`Copy`](../marker/trait.Copy.html),
46+
//! [`Send`](../marker/trait.Send.html),
47+
//! [`Sized`](../marker/trait.Sized.html),
48+
//! [`Sync`](../marker/trait.Sync.html)
49+
//! }.
50+
//! The marker traits indicate fundamental properties of types.
51+
//! * `std::ops::`{
52+
//! [`Drop`](../ops/trait.Drop.html),
53+
//! [`Fn`](../ops/trait.Fn.html),
54+
//! [`FnMut`](../ops/trait.FnMut.html),
55+
//! [`FnOnce`](../ops/trait.FnOnce.html)
56+
//! }.
57+
//! The [destructor][book-dtor] trait and the
58+
//! [closure][book-closures] traits, reexported from the same
59+
//! [module that also defines overloaded
60+
//! operators](../ops/index.html).
61+
//! * `std::mem::`[`drop`](../mem/fn.drop.html).
62+
//! A convenience function for explicitly dropping a value.
63+
//! * `std::boxed::`[`Box`](../boxed/struct.Box.html).
64+
//! The owned heap pointer.
65+
//! * `std::borrow::`[`ToOwned`](../borrow/trait.ToOwned.html).
66+
//! The conversion trait that defines `to_owned`, the generic method
67+
//! for creating an owned type from a borrowed type.
68+
//! * `std::clone::`[`Clone`](../clone/trait.Clone.html).
69+
//! The ubiquitous trait that defines `clone`, the method for
70+
//! producing copies of values that are consider expensive to copy.
71+
//! * `std::cmp::`{
72+
//! [`PartialEq`](../cmp/trait.PartialEq.html),
73+
//! [`PartialOrd`](../cmp/trait.PartialOrd.html),
74+
//! [`Eq`](../cmp/trait.Eq.html),
75+
//! [`Ord`](../cmp/trait.Ord.html)
76+
//! }.
77+
//! The comparision traits, which implement the comparison operators
78+
//! and are often seen in trait bounds.
79+
//! * `std::convert::`{
80+
//! [`AsRef`](../convert/trait.AsRef.html),
81+
//! [`AsMut`](../convert/trait.AsMut.html),
82+
//! [`Into`](../convert/trait.Into.html),
83+
//! [`From`](../convert/trait.From.html)
84+
//! }.
85+
//! Generic conversions, used by savvy API authors to create
86+
//! overloaded methods.
87+
//! * `std::default::`[`Default`](../default/trait.Default).
88+
//! Types that have default values.
89+
//! * `std::iter::`{
90+
//! [`Iterator`](../iter/trait.Iterator.html),
91+
//! [`Extend`](../iter/trait.Extend.html),
92+
//! [`IntoIterator`](../iter/trait.IntoIterator.html),
93+
//! [`DoubleEndedIterator`](../iter/trait.DoubleEndedIterator.html),
94+
//! [`ExactSizeIterator`](../iter/trait.ExactSizeIterator.html)
95+
//! }.
96+
//! [Iterators][book-iter].
97+
//! * `std::option::Option::`{
98+
//! [`self`](../option/enum.Option.html),
99+
//! [`Some`](../option/enum.Option.html),
100+
//! [`None`](../option/enum.Option.html)
101+
//! }.
102+
//! The ubiquitous `Option` type and its two [variants][book-enums],
103+
//! `Some` and `None`.
104+
//! * `std::result::Result::`{
105+
//! [`self`](../result/enum.Result.html),
106+
//! [`Some`](../result/enum.Result.html),
107+
//! [`None`](../result/enum.Result.html)
108+
//! }.
109+
//! The ubiquitous `Result` type and its two [variants][book-enums],
110+
//! `Ok` and `Err`.
111+
//! * `std::slice::`[`SliceConcatExt`](../slice/trait.SliceConcatExt.html).
112+
//! An unstable extension to slices that shouldn't have to exist.
113+
//! * `std::string::`{
114+
//! [`String`](../string/struct.String.html),
115+
//! [`ToString`](../string/trait.ToString.html)
116+
//! }.
117+
//! Heap allocated strings.
118+
//! * `std::vec::`[`Vec`](../vec/struct.Vec.html).
119+
//! Heap allocated vectors.
120+
//!
121+
//! [book-traits]: ../../book/traits.html
122+
//! [book-closures]: ../../book/closures.html
123+
//! [book-dtor]: ../../book/drop.html
124+
//! [book-iter]: ../../book/iterators.html
125+
//! [book-enums]: ../../book/enums.html
37126
38127
#![stable(feature = "rust1", since = "1.0.0")]
39128

0 commit comments

Comments
 (0)