Skip to content

Commit 8e26ad0

Browse files
Giles CopeCentril1tgrpetertoddDylan-DPC
committed
Keyword docs
Co-Authored-By: Mazdak Farrokhzad <[email protected]> Co-Authored-By: Tim Robinson <[email protected]> Co-Authored-By: Peter Todd <[email protected]> Co-Authored-By: Dylan DPC <[email protected]>
1 parent 84d8f9d commit 8e26ad0

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

src/libstd/keyword_docs.rs

+44-5
Original file line numberDiff line numberDiff line change
@@ -1100,10 +1100,28 @@ mod trait_keyword {}
11001100
//
11011101
/// A value of type [`bool`] representing logical **true**.
11021102
///
1103-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1103+
/// Logically `true` is not equal to [`false`].
1104+
///
1105+
/// ## Control structures that check for **true**
1106+
///
1107+
/// Several of Rust's control structures will check for a `bool` condition evaluating to **true**.
1108+
///
1109+
/// * The condition in an [`if`] expression must be of type `bool`.
1110+
/// Whenever that condition evaluates to **true**, the `if` expression takes
1111+
/// on the value of the first block. If however, the condition evaluates
1112+
/// to `false`, the expression takes on value of the `else` block if there is one.
11041113
///
1114+
/// * [`while`] is another control flow construct expecting a `bool`-typed condition.
1115+
/// As long as the condition evaluates to **true**, the `while` loop will continually
1116+
/// evaluate its associated block.
1117+
///
1118+
/// * [`match`] arms can have guard clauses on them.
1119+
///
1120+
/// [`if`]: keyword.if.html
1121+
/// [`while`]: keyword.while.html
1122+
/// [`match`]: ../reference/expressions/match-expr.html#match-guards
1123+
/// [`false`]: keyword.false.html
11051124
/// [`bool`]: primitive.bool.html
1106-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
11071125
mod true_keyword {}
11081126

11091127
#[doc(keyword = "type")]
@@ -1167,12 +1185,33 @@ mod await_keyword {}
11671185

11681186
#[doc(keyword = "dyn")]
11691187
//
1170-
/// Name the type of a [trait object].
1188+
/// `dyn` is a prefix of a [trait object]'s type.
11711189
///
1172-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1190+
/// The `dyn` keyword is used to highlight that calls to methods on the associated `Trait`
1191+
/// are dynamically dispatched. To use the trait this way, it must be 'object safe'.
1192+
///
1193+
/// Unlike generic parameters or `impl Trait`, the compiler does not know the concrete type that
1194+
/// is being passed. That is, the type has been [erased].
1195+
/// As such, a `dyn Trait` reference contains _two_ pointers.
1196+
/// One pointer goes to the data (e.g., an instance of a struct).
1197+
/// Another pointer goes to a map of method call names to function pointers
1198+
/// (known as a virtual method table or vtable).
1199+
///
1200+
/// At run-time, when a method needs to be called on the `dyn Trait`, the vtable is consulted to get
1201+
/// the function pointer and then that function pointer is called.
1202+
///
1203+
/// ## Trade-offs
1204+
///
1205+
/// The above indirection is the additional runtime cost of calling a function on a `dyn Trait`.
1206+
/// Methods called by dynamic dispatch generally cannot be inlined by the compiler.
1207+
///
1208+
/// However, `dyn Trait` is likely to produce smaller code than `impl Trait` / generic parameters as
1209+
/// the method won't be duplicated for each concrete type.
1210+
///
1211+
/// Read more about `object safety` and [trait object]s.
11731212
///
11741213
/// [trait object]: ../book/ch17-02-trait-objects.html
1175-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1214+
/// [erased]: https://en.wikipedia.org/wiki/Type_erasure
11761215
mod dyn_keyword {}
11771216

11781217
#[doc(keyword = "union")]

0 commit comments

Comments
 (0)