diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-08-22 11:45:40 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-22 11:45:40 +0530 |
| commit | a4950ef7eb1bc72ede47775deea4018c7b62d40a (patch) | |
| tree | fc58a8cbd250c84e6d6327f77457c520bf7259f4 /library/std/src | |
| parent | d0ea1d767925d53b2230e2ba81197821514781f0 (diff) | |
| parent | 17ddcb434b8fca7eeb865985942b146647a3510f (diff) | |
| download | rust-a4950ef7eb1bc72ede47775deea4018c7b62d40a.tar.gz rust-a4950ef7eb1bc72ede47775deea4018c7b62d40a.zip | |
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.
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/error.rs | 2 | ||||
| -rw-r--r-- | library/std/src/f32.rs | 2 | ||||
| -rw-r--r-- | library/std/src/f64.rs | 2 | ||||
| -rw-r--r-- | library/std/src/primitive_docs.rs | 61 |
4 files changed, 56 insertions, 11 deletions
diff --git a/library/std/src/error.rs b/library/std/src/error.rs index df7a49d2582..4fbcfd85d7c 100644 --- a/library/std/src/error.rs +++ b/library/std/src/error.rs @@ -1,4 +1,4 @@ -//! Interfaces for working with Errors. +//! The `Error` trait provides common functionality for errors. //! //! # Error Handling In Rust //! diff --git a/library/std/src/f32.rs b/library/std/src/f32.rs index 933b52b4dcc..3dd5b12507f 100644 --- a/library/std/src/f32.rs +++ b/library/std/src/f32.rs @@ -1,4 +1,4 @@ -//! Constants specific to the `f32` single-precision floating point type. +//! Constants for the `f32` single-precision floating point type. //! //! *[See also the `f32` primitive type](primitive@f32).* //! diff --git a/library/std/src/f64.rs b/library/std/src/f64.rs index a9aa84f70d1..31351a87978 100644 --- a/library/std/src/f64.rs +++ b/library/std/src/f64.rs @@ -1,4 +1,4 @@ -//! Constants specific to the `f64` double-precision floating point type. +//! Constants for the `f64` double-precision floating point type. //! //! *[See also the `f64` primitive type](primitive@f64).* //! diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 1c14b9341ca..2b2ef64fdb1 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -801,11 +801,53 @@ mod prim_array {} /// assert_eq!(2 * pointer_size, std::mem::size_of::<Box<[u8]>>()); /// assert_eq!(2 * pointer_size, std::mem::size_of::<Rc<[u8]>>()); /// ``` +/// +/// ## Trait Implementations +/// +/// Some traits are implemented for slices if the element type implements +/// that trait. This includes [`Eq`], [`Hash`] and [`Ord`]. +/// +/// ## Iteration +/// +/// The slices implement `IntoIterator`. The iterator yields references to the +/// slice elements. +/// +/// ``` +/// let numbers: &[i32] = &[0, 1, 2]; +/// for n in numbers { +/// println!("{n} is a number!"); +/// } +/// ``` +/// +/// The mutable slice yields mutable references to the elements: +/// +/// ``` +/// let mut scores: &mut [i32] = &mut [7, 8, 9]; +/// for score in scores { +/// *score += 1; +/// } +/// ``` +/// +/// This iterator yields mutable references to the slice's elements, so while +/// the element type of the slice is `i32`, the element type of the iterator is +/// `&mut i32`. +/// +/// * [`.iter`] and [`.iter_mut`] are the explicit methods to return the default +/// iterators. +/// * Further methods that return iterators are [`.split`], [`.splitn`], +/// [`.chunks`], [`.windows`] and more. +/// +/// [`Hash`]: core::hash::Hash +/// [`.iter`]: slice::iter +/// [`.iter_mut`]: slice::iter_mut +/// [`.split`]: slice::split +/// [`.splitn`]: slice::splitn +/// [`.chunks`]: slice::chunks +/// [`.windows`]: slice::windows #[stable(feature = "rust1", since = "1.0.0")] mod prim_slice {} #[doc(primitive = "str")] -// /// String slices. /// /// *[See also the `std::str` module](crate::str).* @@ -816,19 +858,22 @@ mod prim_slice {} /// /// String slices are always valid UTF-8. /// -/// # Examples +/// # Basic Usage /// /// String literals are string slices: /// /// ``` -/// let hello = "Hello, world!"; -/// -/// // with an explicit type annotation -/// let hello: &'static str = "Hello, world!"; +/// let hello_world = "Hello, World!"; /// ``` /// -/// They are `'static` because they're stored directly in the final binary, and -/// so will be valid for the `'static` duration. +/// Here we have declared a string slice initialized with a string literal. +/// String literals have a static lifetime, which means the string `hello_world` +/// is guaranteed to be valid for the duration of the entire program. +/// We can explicitly specify `hello_world`'s lifetime as well: +/// +/// ``` +/// let hello_world: &'static str = "Hello, world!"; +/// ``` /// /// # Representation /// |
