Skip to content

Commit a4950ef

Browse files
authored
Rollup merge of #93162 - camsteffen:std-prim-docs, r=Mark-Simulacrum
Std module docs improvements My primary goal is to create a cleaner separation between primitive types and primitive type helper modules (fixes #92777). I also changed a few header lines in other top-level std modules (seen at https://doc.rust-lang.org/std/) for consistency. Some conventions used/established: * "The \`Box\<T>` type for heap allocation." - if a module mainly provides a single type, name it and summarize its purpose in the module header * "Utilities for the _ primitive type." - this wording is used for the header of helper modules * Documentation for primitive types themselves are removed from helper modules * provided-by-core functionality of primitive types is documented in the primitive type instead of the helper module (such as the "Iteration" section in the slice docs) I wonder if some content in `std::ptr` should be in `pointer` but I did not address this.
2 parents d0ea1d7 + 17ddcb4 commit a4950ef

File tree

16 files changed

+127
-128
lines changed

16 files changed

+127
-128
lines changed

library/alloc/src/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A pointer type for heap allocation.
1+
//! The `Box<T>` type for heap allocation.
22
//!
33
//! [`Box<T>`], casually referred to as a 'box', provides the simplest form of
44
//! heap allocation in Rust. Boxes provide ownership for this allocation, and

library/alloc/src/slice.rs

+5-75
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,12 @@
1-
//! A dynamically-sized view into a contiguous sequence, `[T]`.
1+
//! Utilities for the slice primitive type.
22
//!
33
//! *[See also the slice primitive type](slice).*
44
//!
5-
//! Slices are a view into a block of memory represented as a pointer and a
6-
//! length.
5+
//! Most of the structs in this module are iterator types which can only be created
6+
//! using a certain function. For example, `slice.iter()` yields an [`Iter`].
77
//!
8-
//! ```
9-
//! // slicing a Vec
10-
//! let vec = vec![1, 2, 3];
11-
//! let int_slice = &vec[..];
12-
//! // coercing an array to a slice
13-
//! let str_slice: &[&str] = &["one", "two", "three"];
14-
//! ```
15-
//!
16-
//! Slices are either mutable or shared. The shared slice type is `&[T]`,
17-
//! while the mutable slice type is `&mut [T]`, where `T` represents the element
18-
//! type. For example, you can mutate the block of memory that a mutable slice
19-
//! points to:
20-
//!
21-
//! ```
22-
//! let x = &mut [1, 2, 3];
23-
//! x[1] = 7;
24-
//! assert_eq!(x, &[1, 7, 3]);
25-
//! ```
26-
//!
27-
//! Here are some of the things this module contains:
28-
//!
29-
//! ## Structs
30-
//!
31-
//! There are several structs that are useful for slices, such as [`Iter`], which
32-
//! represents iteration over a slice.
33-
//!
34-
//! ## Trait Implementations
35-
//!
36-
//! There are several implementations of common traits for slices. Some examples
37-
//! include:
38-
//!
39-
//! * [`Clone`]
40-
//! * [`Eq`], [`Ord`] - for slices whose element type are [`Eq`] or [`Ord`].
41-
//! * [`Hash`] - for slices whose element type is [`Hash`].
42-
//!
43-
//! ## Iteration
44-
//!
45-
//! The slices implement `IntoIterator`. The iterator yields references to the
46-
//! slice elements.
47-
//!
48-
//! ```
49-
//! let numbers = &[0, 1, 2];
50-
//! for n in numbers {
51-
//! println!("{n} is a number!");
52-
//! }
53-
//! ```
54-
//!
55-
//! The mutable slice yields mutable references to the elements:
56-
//!
57-
//! ```
58-
//! let mut scores = [7, 8, 9];
59-
//! for score in &mut scores[..] {
60-
//! *score += 1;
61-
//! }
62-
//! ```
63-
//!
64-
//! This iterator yields mutable references to the slice's elements, so while
65-
//! the element type of the slice is `i32`, the element type of the iterator is
66-
//! `&mut i32`.
67-
//!
68-
//! * [`.iter`] and [`.iter_mut`] are the explicit methods to return the default
69-
//! iterators.
70-
//! * Further methods that return iterators are [`.split`], [`.splitn`],
71-
//! [`.chunks`], [`.windows`] and more.
72-
//!
73-
//! [`Hash`]: core::hash::Hash
74-
//! [`.iter`]: slice::iter
75-
//! [`.iter_mut`]: slice::iter_mut
76-
//! [`.split`]: slice::split
77-
//! [`.splitn`]: slice::splitn
78-
//! [`.chunks`]: slice::chunks
79-
//! [`.windows`]: slice::windows
8+
//! A few functions are provided to create a slice from a value reference
9+
//! or from a raw pointer.
8010
#![stable(feature = "rust1", since = "1.0.0")]
8111
// Many of the usings in this module are only used in the test configuration.
8212
// It's cleaner to just turn off the unused_imports warning than to fix them.

library/alloc/src/str.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
1-
//! Unicode string slices.
1+
//! Utilities for the `str` primitive type.
22
//!
33
//! *[See also the `str` primitive type](str).*
4-
//!
5-
//! The `&str` type is one of the two main string types, the other being `String`.
6-
//! Unlike its `String` counterpart, its contents are borrowed.
7-
//!
8-
//! # Basic Usage
9-
//!
10-
//! A basic string declaration of `&str` type:
11-
//!
12-
//! ```
13-
//! let hello_world = "Hello, World!";
14-
//! ```
15-
//!
16-
//! Here we have declared a string literal, also known as a string slice.
17-
//! String literals have a static lifetime, which means the string `hello_world`
18-
//! is guaranteed to be valid for the duration of the entire program.
19-
//! We can explicitly specify `hello_world`'s lifetime as well:
20-
//!
21-
//! ```
22-
//! let hello_world: &'static str = "Hello, world!";
23-
//! ```
244
255
#![stable(feature = "rust1", since = "1.0.0")]
266
// Many of the usings in this module are only used in the test configuration.

library/core/src/any.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
//! This module contains the `Any` trait, which enables dynamic typing
2-
//! of any `'static` type through runtime reflection. It also contains the
3-
//! `Provider` trait and accompanying API, which enable trait objects to provide
4-
//! data based on typed requests, an alternate form of runtime reflection.
1+
//! Utilities for dynamic typing or type reflection.
52
//!
63
//! # `Any` and `TypeId`
74
//!

library/core/src/array/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Helper functions and types for fixed-length arrays.
1+
//! Utilities for the array primitive type.
22
//!
33
//! *[See also the array primitive type](array).*
44

library/core/src/borrow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A module for working with borrowed data.
1+
//! Utilities for working with borrowed data.
22
33
#![stable(feature = "rust1", since = "1.0.0")]
44

library/core/src/char/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//! A character type.
1+
//! Utilities for the `char` primitive type.
2+
//!
3+
//! *[See also the `char` primitive type](primitive@char).*
24
//!
35
//! The `char` type represents a single character. More specifically, since
46
//! 'character' isn't a well-defined concept in Unicode, `char` is a '[Unicode

library/core/src/cmp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! Functionality for ordering and comparison.
1+
//! Utilities for comparing and ordering values.
22
//!
3-
//! This module contains various tools for ordering and comparing values. In
3+
//! This module contains various tools for comparing and ordering values. In
44
//! summary:
55
//!
66
//! * [`Eq`] and [`PartialEq`] are traits that allow you to define total and

library/core/src/default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! The `Default` trait for types which may have meaningful default values.
1+
//! The `Default` trait for types with a default value.
22
33
#![stable(feature = "rust1", since = "1.0.0")]
44

library/core/src/num/f32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Constants specific to the `f32` single-precision floating point type.
1+
//! Constants for the `f32` single-precision floating point type.
22
//!
33
//! *[See also the `f32` primitive type][f32].*
44
//!

library/core/src/num/f64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Constants specific to the `f64` double-precision floating point type.
1+
//! Constants for the `f64` double-precision floating point type.
22
//!
33
//! *[See also the `f64` primitive type][f64].*
44
//!

library/core/src/primitive_docs.rs

+53-8
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,53 @@ mod prim_array {}
801801
/// assert_eq!(2 * pointer_size, std::mem::size_of::<Box<[u8]>>());
802802
/// assert_eq!(2 * pointer_size, std::mem::size_of::<Rc<[u8]>>());
803803
/// ```
804+
///
805+
/// ## Trait Implementations
806+
///
807+
/// Some traits are implemented for slices if the element type implements
808+
/// that trait. This includes [`Eq`], [`Hash`] and [`Ord`].
809+
///
810+
/// ## Iteration
811+
///
812+
/// The slices implement `IntoIterator`. The iterator yields references to the
813+
/// slice elements.
814+
///
815+
/// ```
816+
/// let numbers: &[i32] = &[0, 1, 2];
817+
/// for n in numbers {
818+
/// println!("{n} is a number!");
819+
/// }
820+
/// ```
821+
///
822+
/// The mutable slice yields mutable references to the elements:
823+
///
824+
/// ```
825+
/// let mut scores: &mut [i32] = &mut [7, 8, 9];
826+
/// for score in scores {
827+
/// *score += 1;
828+
/// }
829+
/// ```
830+
///
831+
/// This iterator yields mutable references to the slice's elements, so while
832+
/// the element type of the slice is `i32`, the element type of the iterator is
833+
/// `&mut i32`.
834+
///
835+
/// * [`.iter`] and [`.iter_mut`] are the explicit methods to return the default
836+
/// iterators.
837+
/// * Further methods that return iterators are [`.split`], [`.splitn`],
838+
/// [`.chunks`], [`.windows`] and more.
839+
///
840+
/// [`Hash`]: core::hash::Hash
841+
/// [`.iter`]: slice::iter
842+
/// [`.iter_mut`]: slice::iter_mut
843+
/// [`.split`]: slice::split
844+
/// [`.splitn`]: slice::splitn
845+
/// [`.chunks`]: slice::chunks
846+
/// [`.windows`]: slice::windows
804847
#[stable(feature = "rust1", since = "1.0.0")]
805848
mod prim_slice {}
806849

807850
#[doc(primitive = "str")]
808-
//
809851
/// String slices.
810852
///
811853
/// *[See also the `std::str` module](crate::str).*
@@ -816,19 +858,22 @@ mod prim_slice {}
816858
///
817859
/// String slices are always valid UTF-8.
818860
///
819-
/// # Examples
861+
/// # Basic Usage
820862
///
821863
/// String literals are string slices:
822864
///
823865
/// ```
824-
/// let hello = "Hello, world!";
825-
///
826-
/// // with an explicit type annotation
827-
/// let hello: &'static str = "Hello, world!";
866+
/// let hello_world = "Hello, World!";
828867
/// ```
829868
///
830-
/// They are `'static` because they're stored directly in the final binary, and
831-
/// so will be valid for the `'static` duration.
869+
/// Here we have declared a string slice initialized with a string literal.
870+
/// String literals have a static lifetime, which means the string `hello_world`
871+
/// is guaranteed to be valid for the duration of the entire program.
872+
/// We can explicitly specify `hello_world`'s lifetime as well:
873+
///
874+
/// ```
875+
/// let hello_world: &'static str = "Hello, world!";
876+
/// ```
832877
///
833878
/// # Representation
834879
///

library/std/src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Interfaces for working with Errors.
1+
//! The `Error` trait provides common functionality for errors.
22
//!
33
//! # Error Handling In Rust
44
//!

library/std/src/f32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Constants specific to the `f32` single-precision floating point type.
1+
//! Constants for the `f32` single-precision floating point type.
22
//!
33
//! *[See also the `f32` primitive type](primitive@f32).*
44
//!

library/std/src/f64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Constants specific to the `f64` double-precision floating point type.
1+
//! Constants for the `f64` double-precision floating point type.
22
//!
33
//! *[See also the `f64` primitive type](primitive@f64).*
44
//!

library/std/src/primitive_docs.rs

+53-8
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,53 @@ mod prim_array {}
801801
/// assert_eq!(2 * pointer_size, std::mem::size_of::<Box<[u8]>>());
802802
/// assert_eq!(2 * pointer_size, std::mem::size_of::<Rc<[u8]>>());
803803
/// ```
804+
///
805+
/// ## Trait Implementations
806+
///
807+
/// Some traits are implemented for slices if the element type implements
808+
/// that trait. This includes [`Eq`], [`Hash`] and [`Ord`].
809+
///
810+
/// ## Iteration
811+
///
812+
/// The slices implement `IntoIterator`. The iterator yields references to the
813+
/// slice elements.
814+
///
815+
/// ```
816+
/// let numbers: &[i32] = &[0, 1, 2];
817+
/// for n in numbers {
818+
/// println!("{n} is a number!");
819+
/// }
820+
/// ```
821+
///
822+
/// The mutable slice yields mutable references to the elements:
823+
///
824+
/// ```
825+
/// let mut scores: &mut [i32] = &mut [7, 8, 9];
826+
/// for score in scores {
827+
/// *score += 1;
828+
/// }
829+
/// ```
830+
///
831+
/// This iterator yields mutable references to the slice's elements, so while
832+
/// the element type of the slice is `i32`, the element type of the iterator is
833+
/// `&mut i32`.
834+
///
835+
/// * [`.iter`] and [`.iter_mut`] are the explicit methods to return the default
836+
/// iterators.
837+
/// * Further methods that return iterators are [`.split`], [`.splitn`],
838+
/// [`.chunks`], [`.windows`] and more.
839+
///
840+
/// [`Hash`]: core::hash::Hash
841+
/// [`.iter`]: slice::iter
842+
/// [`.iter_mut`]: slice::iter_mut
843+
/// [`.split`]: slice::split
844+
/// [`.splitn`]: slice::splitn
845+
/// [`.chunks`]: slice::chunks
846+
/// [`.windows`]: slice::windows
804847
#[stable(feature = "rust1", since = "1.0.0")]
805848
mod prim_slice {}
806849

807850
#[doc(primitive = "str")]
808-
//
809851
/// String slices.
810852
///
811853
/// *[See also the `std::str` module](crate::str).*
@@ -816,19 +858,22 @@ mod prim_slice {}
816858
///
817859
/// String slices are always valid UTF-8.
818860
///
819-
/// # Examples
861+
/// # Basic Usage
820862
///
821863
/// String literals are string slices:
822864
///
823865
/// ```
824-
/// let hello = "Hello, world!";
825-
///
826-
/// // with an explicit type annotation
827-
/// let hello: &'static str = "Hello, world!";
866+
/// let hello_world = "Hello, World!";
828867
/// ```
829868
///
830-
/// They are `'static` because they're stored directly in the final binary, and
831-
/// so will be valid for the `'static` duration.
869+
/// Here we have declared a string slice initialized with a string literal.
870+
/// String literals have a static lifetime, which means the string `hello_world`
871+
/// is guaranteed to be valid for the duration of the entire program.
872+
/// We can explicitly specify `hello_world`'s lifetime as well:
873+
///
874+
/// ```
875+
/// let hello_world: &'static str = "Hello, world!";
876+
/// ```
832877
///
833878
/// # Representation
834879
///

0 commit comments

Comments
 (0)