10
10
11
11
#[ doc( keyword = "as" ) ]
12
12
//
13
- /// The type coercion keyword .
13
+ /// The keyword for casting types .
14
14
///
15
15
/// `as` is most commonly used to turn primitive types into other primitive types, but it has other
16
16
/// uses that include turning pointers into addresses, addresses into pointers, and pointers into
24
24
/// assert_eq!(true as u8 + thing2 as u8, 100);
25
25
/// ```
26
26
///
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.
32
35
///
33
36
/// For more information on what `as` is capable of, see the [Reference]
34
37
///
35
38
/// [Reference]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
39
+ /// [`crate`]: keyword.crate.html
40
+ /// [`use`]: keyword.use.html
36
41
mod as_keyword { }
37
42
38
43
#[ doc( keyword = "const" ) ]
@@ -52,12 +57,12 @@ mod as_keyword { }
52
57
///
53
58
/// Constants must be explicitly typed, unlike with `let` you can't ignore its type and let the
54
59
/// 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`.
57
62
///
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:
61
66
///
62
67
/// ```rust
63
68
/// const WORDS: &'static str = "hello rust!";
@@ -73,9 +78,8 @@ mod as_keyword { }
73
78
/// to which one should be used at which times. To put it simply, constants are inlined wherever
74
79
/// they're used, making using them identical to simply replacing the name of the const with its
75
80
/// 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.
79
83
///
80
84
/// Constants, as with statics, should always be in SCREAMING_SNAKE_CASE.
81
85
///
@@ -130,8 +134,8 @@ mod crate_keyword { }
130
134
///
131
135
/// Enums in Rust are similar to those of other compiled languages like C, but have important
132
136
/// 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 .
135
139
///
136
140
/// ```rust
137
141
/// # struct Coord;
@@ -160,9 +164,9 @@ mod crate_keyword { }
160
164
/// ```
161
165
///
162
166
/// 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.
166
170
///
167
171
/// Instantiating enum variants involves explicitly using the enum's name as its namespace,
168
172
/// followed by one of its variants. `SimpleEnum::SecondVariant` would be an example from above.
@@ -188,7 +192,7 @@ mod enum_keyword { }
188
192
/// lazy_static;`. The other use is in foreign function interfaces (FFI).
189
193
///
190
194
/// `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.
192
196
///
193
197
/// ```rust ignore
194
198
/// #[link(name = "my_c_library")]
@@ -197,8 +201,8 @@ mod enum_keyword { }
197
201
/// }
198
202
/// ```
199
203
///
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
202
206
/// code could then use `my_c_function` as if it were any other unsafe Rust function. Working with
203
207
/// non-Rust languages and FFI is inherently unsafe, so wrappers are usually built around C APIs.
204
208
///
@@ -275,7 +279,8 @@ mod extern_keyword { }
275
279
/// ```
276
280
///
277
281
/// 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.
279
284
///
280
285
/// Along with being made public via [`pub`], `fn` can also have an [`extern`] added for use in
281
286
/// FFI.
@@ -475,8 +480,8 @@ mod let_keyword { }
475
480
//
476
481
/// The keyword used to define structs.
477
482
///
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.
480
485
///
481
486
/// ```rust
482
487
/// struct Regular {
@@ -487,7 +492,7 @@ mod let_keyword { }
487
492
///
488
493
/// struct Tuple(u32, String);
489
494
///
490
- /// struct Empty ;
495
+ /// struct Unit ;
491
496
/// ```
492
497
///
493
498
/// Regular structs are the most commonly used. Each field defined within them has a name and a
@@ -501,14 +506,14 @@ mod let_keyword { }
501
506
/// individual variables, the same syntax is used as with regular tuples, namely `foo.0`, `foo.1`,
502
507
/// etc, starting at zero.
503
508
///
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.
508
513
///
509
514
/// # Instantiation
510
515
///
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
512
517
/// matched as needed. The most common way to make a new struct is via a constructor method such as
513
518
/// `new()`, but when that isn't available (or you're writing the constructor itself), struct
514
519
/// literal syntax is used:
0 commit comments