Skip to content

Commit 5393b27

Browse files
committed
Incorporate keyword doc PR critique
1 parent 5d05ae7 commit 5393b27

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

src/libstd/keyword_docs.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#[doc(keyword = "as")]
1212
//
13-
/// The type coercion keyword.
13+
/// The keyword for casting types.
1414
///
1515
/// `as` is most commonly used to turn primitive types into other primitive types, but it has other
1616
/// uses that include turning pointers into addresses, addresses into pointers, and pointers into
@@ -24,15 +24,20 @@
2424
/// assert_eq!(true as u8 + thing2 as u8, 100);
2525
/// ```
2626
///
27-
/// In general, any coercion that can be performed via writing out type hints can also be done
28-
/// using `as`, so instead of writing `let x: u32 = 123`, you can write `let x = 123 as u32` (Note:
29-
/// `let x = 123u32` would be best in that situation). The same is not true in the other direction,
30-
/// however, explicitly using `as` allows a few more coercions that aren't allowed implicitly, such
31-
/// as changing the type of a raw pointer or turning closures into raw pointers.
27+
/// In general, any cast that can be performed via ascribing the type can also be done using `as`,
28+
/// so instead of writing `let x: u32 = 123`, you can write `let x = 123 as u32` (Note: `let x: u32
29+
/// = 123` would be best in that situation). The same is not true in the other direction, however,
30+
/// explicitly using `as` allows a few more coercions that aren't allowed implicitly, such as
31+
/// changing the type of a raw pointer or turning closures into raw pointers.
32+
///
33+
/// Other places `as` is used include as extra syntax for [`crate`] and [`use`], to change the name
34+
/// something is imported as.
3235
///
3336
/// For more information on what `as` is capable of, see the [Reference]
3437
///
3538
/// [Reference]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
39+
/// [`crate`]: keyword.crate.html
40+
/// [`use`]: keyword.use.html
3641
mod as_keyword { }
3742

3843
#[doc(keyword = "const")]
@@ -52,12 +57,12 @@ mod as_keyword { }
5257
///
5358
/// Constants must be explicitly typed, unlike with `let` you can't ignore its type and let the
5459
/// compiler figure it out. Any constant value can be defined in a const, which in practice happens
55-
/// to be most things that would be reasonable to have a constant. For example, you can't have a
56-
/// File as a `const`.
60+
/// to be most things that would be reasonable to have a constant (barring `const fn`s, coming
61+
/// soon). For example, you can't have a File as a `const`.
5762
///
58-
/// The only lifetime allowed in a constant is 'static, which is the lifetime that encompasses all
59-
/// others in a Rust program. For example, if you wanted to define a constant string, it would look
60-
/// like this:
63+
/// The only lifetime allowed in a constant is `'static`, which is the lifetime that encompasses
64+
/// all others in a Rust program. For example, if you wanted to define a constant string, it would
65+
/// look like this:
6166
///
6267
/// ```rust
6368
/// const WORDS: &'static str = "hello rust!";
@@ -73,9 +78,8 @@ mod as_keyword { }
7378
/// to which one should be used at which times. To put it simply, constants are inlined wherever
7479
/// they're used, making using them identical to simply replacing the name of the const with its
7580
/// value. Static variables on the other hand point to a single location in memory, which all
76-
/// accesses share. This means that, unlike with constants, they can't have destructors, but it
77-
/// also means that (via unsafe code) they can be mutable, which is useful for the rare situations
78-
/// in which you can't avoid using global state.
81+
/// accesses share. This means that, unlike with constants, they can't have destructors, and act as
82+
/// a single value across the entire codebase.
7983
///
8084
/// Constants, as with statics, should always be in SCREAMING_SNAKE_CASE.
8185
///
@@ -130,8 +134,8 @@ mod crate_keyword { }
130134
///
131135
/// Enums in Rust are similar to those of other compiled languages like C, but have important
132136
/// differences that make them considerably more powerful. What Rust calls enums are more commonly
133-
/// known as Algebraic Data Types if you're coming from a functional programming background, but
134-
/// the important part is that data can go with the enum variants.
137+
/// known as Algebraic Data Types if you're coming from a functional programming background. The
138+
/// important detail is that each enum variant can have data to go along with it.
135139
///
136140
/// ```rust
137141
/// # struct Coord;
@@ -160,9 +164,9 @@ mod crate_keyword { }
160164
/// ```
161165
///
162166
/// The first enum shown is the usual kind of enum you'd find in a C-style language. The second
163-
/// shows off a hypothetical example of something storing location data, with Coord being any other
164-
/// type that's needed, for example a struct. The third example demonstrates the kind of variant a
165-
/// variant can store, ranging from nothing, to a tuple, to an anonymous struct.
167+
/// shows off a hypothetical example of something storing location data, with `Coord` being any
168+
/// other type that's needed, for example a struct. The third example demonstrates the kind of
169+
/// data a variant can store, ranging from nothing, to a tuple, to an anonymous struct.
166170
///
167171
/// Instantiating enum variants involves explicitly using the enum's name as its namespace,
168172
/// followed by one of its variants. `SimpleEnum::SecondVariant` would be an example from above.
@@ -188,7 +192,7 @@ mod enum_keyword { }
188192
/// lazy_static;`. The other use is in foreign function interfaces (FFI).
189193
///
190194
/// `extern` is used in two different contexts within FFI. The first is in the form of external
191-
/// blcoks, for declaring function interfaces that Rust code can call foreign code by.
195+
/// blocks, for declaring function interfaces that Rust code can call foreign code by.
192196
///
193197
/// ```rust ignore
194198
/// #[link(name = "my_c_library")]
@@ -197,8 +201,8 @@ mod enum_keyword { }
197201
/// }
198202
/// ```
199203
///
200-
/// This code would attempt to link with libmy_c_library.so on unix-like systems and
201-
/// my_c_library.dll on Windows at runtime, and panic if it can't find something to link to. Rust
204+
/// This code would attempt to link with `libmy_c_library.so` on unix-like systems and
205+
/// `my_c_library.dll` on Windows at runtime, and panic if it can't find something to link to. Rust
202206
/// code could then use `my_c_function` as if it were any other unsafe Rust function. Working with
203207
/// non-Rust languages and FFI is inherently unsafe, so wrappers are usually built around C APIs.
204208
///
@@ -275,7 +279,8 @@ mod extern_keyword { }
275279
/// ```
276280
///
277281
/// Declaring trait bounds in the angle brackets is functionally identical to using a [`where`]
278-
/// clause, but `where` is preferred due to it being easier to understand at a glance.
282+
/// clause. It's up to the programmer to decide which works better in each situation, but `where`
283+
/// tends to be better when things get longer than one line.
279284
///
280285
/// Along with being made public via [`pub`], `fn` can also have an [`extern`] added for use in
281286
/// FFI.
@@ -475,8 +480,8 @@ mod let_keyword { }
475480
//
476481
/// The keyword used to define structs.
477482
///
478-
/// Structs in Rust come in three flavours: Regular structs, tuple structs,
479-
/// and empty structs.
483+
/// Structs in Rust come in three flavours: Structs with named fields, tuple structs, and unit
484+
/// structs.
480485
///
481486
/// ```rust
482487
/// struct Regular {
@@ -487,7 +492,7 @@ mod let_keyword { }
487492
///
488493
/// struct Tuple(u32, String);
489494
///
490-
/// struct Empty;
495+
/// struct Unit;
491496
/// ```
492497
///
493498
/// Regular structs are the most commonly used. Each field defined within them has a name and a
@@ -501,14 +506,14 @@ mod let_keyword { }
501506
/// individual variables, the same syntax is used as with regular tuples, namely `foo.0`, `foo.1`,
502507
/// etc, starting at zero.
503508
///
504-
/// Empty structs, or unit-like structs, are most commonly used as markers, for example
505-
/// [`PhantomData`]. Empty structs have a size of zero bytes, but unlike empty enums they can be
506-
/// instantiated, making them similar to the unit type `()`. Unit-like structs are useful when you
507-
/// need to implement a trait on something, but don't need to store any data inside it.
509+
/// Unit structs are most commonly used as marker. They have a size of zero bytes, but unlike empty
510+
/// enums they can be instantiated, making them isomorphic to the unit type `()`. Unit structs are
511+
/// useful when you need to implement a trait on something, but don't need to store any data inside
512+
/// it.
508513
///
509514
/// # Instantiation
510515
///
511-
/// Structs can be instantiated in a manner of different ways, each of which can be mixed and
516+
/// Structs can be instantiated in different ways, all of which can be mixed and
512517
/// matched as needed. The most common way to make a new struct is via a constructor method such as
513518
/// `new()`, but when that isn't available (or you're writing the constructor itself), struct
514519
/// literal syntax is used:

0 commit comments

Comments
 (0)