]> git.lizzy.rs Git - rust.git/blob - src/libstd/keyword_docs.rs
Rollup merge of #65191 - varkor:const-generics-test-cases, r=nikomatsakis
[rust.git] / src / libstd / keyword_docs.rs
1 #[doc(keyword = "as")]
2 //
3 /// Cast between types, or rename an import.
4 ///
5 /// `as` is most commonly used to turn primitive types into other primitive types, but it has other
6 /// uses that include turning pointers into addresses, addresses into pointers, and pointers into
7 /// other pointers.
8 ///
9 /// ```rust
10 /// let thing1: u8 = 89.0 as u8;
11 /// assert_eq!('B' as u32, 66);
12 /// assert_eq!(thing1 as char, 'Y');
13 /// let thing2: f32 = thing1 as f32 + 10.5;
14 /// assert_eq!(true as u8 + thing2 as u8, 100);
15 /// ```
16 ///
17 /// In general, any cast that can be performed via ascribing the type can also be done using `as`,
18 /// so instead of writing `let x: u32 = 123`, you can write `let x = 123 as u32` (Note: `let x: u32
19 /// = 123` would be best in that situation). The same is not true in the other direction, however,
20 /// explicitly using `as` allows a few more coercions that aren't allowed implicitly, such as
21 /// changing the type of a raw pointer or turning closures into raw pointers.
22 ///
23 /// Other places `as` is used include as extra syntax for [`crate`] and `use`, to change the name
24 /// something is imported as.
25 ///
26 /// For more information on what `as` is capable of, see the [Reference]
27 ///
28 /// [Reference]: ../reference/expressions/operator-expr.html#type-cast-expressions
29 /// [`crate`]: keyword.crate.html
30 mod as_keyword { }
31
32 #[doc(keyword = "break")]
33 //
34 /// Exit early from a loop.
35 ///
36 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
37 ///
38 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
39 mod break_keyword { }
40
41 #[doc(keyword = "const")]
42 //
43 /// Compile-time constants and deterministic functions.
44 ///
45 /// Sometimes a certain value is used many times throughout a program, and it can become
46 /// inconvenient to copy it over and over. What's more, it's not always possible or desirable to
47 /// make it a variable that gets carried around to each function that needs it. In these cases, the
48 /// `const` keyword provides a convenient alternative to code duplication.
49 ///
50 /// ```rust
51 /// const THING: u32 = 0xABAD1DEA;
52 ///
53 /// let foo = 123 + THING;
54 /// ```
55 ///
56 /// Constants must be explicitly typed, unlike with `let` you can't ignore its type and let the
57 /// compiler figure it out. Any constant value can be defined in a const, which in practice happens
58 /// to be most things that would be reasonable to have a constant (barring `const fn`s). For
59 /// example, you can't have a File as a `const`.
60 ///
61 /// The only lifetime allowed in a constant is `'static`, which is the lifetime that encompasses
62 /// all others in a Rust program. For example, if you wanted to define a constant string, it would
63 /// look like this:
64 ///
65 /// ```rust
66 /// const WORDS: &str = "hello rust!";
67 /// ```
68 ///
69 /// Thanks to static lifetime elision, you usually don't have to explicitly use 'static:
70 ///
71 /// ```rust
72 /// const WORDS: &str = "hello convenience!";
73 /// ```
74 ///
75 /// `const` items looks remarkably similar to `static` items, which introduces some confusion as
76 /// to which one should be used at which times. To put it simply, constants are inlined wherever
77 /// they're used, making using them identical to simply replacing the name of the const with its
78 /// value. Static variables on the other hand point to a single location in memory, which all
79 /// accesses share. This means that, unlike with constants, they can't have destructors, and act as
80 /// a single value across the entire codebase.
81 ///
82 /// Constants, as with statics, should always be in SCREAMING_SNAKE_CASE.
83 ///
84 /// The `const` keyword is also used in raw pointers in combination with `mut`, as seen in `*const
85 /// T` and `*mut T`. More about that can be read at the [pointer] primitive part of the Rust docs.
86 ///
87 /// For more detail on `const`, see the [Rust Book] or the [Reference]
88 ///
89 /// [pointer]: primitive.pointer.html
90 /// [Rust Book]:
91 /// ../book/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants
92 /// [Reference]: ../reference/items/constant-items.html
93 mod const_keyword { }
94
95 #[doc(keyword = "continue")]
96 //
97 /// Skip to the next iteration of a loop.
98 ///
99 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
100 ///
101 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
102 mod continue_keyword { }
103
104 #[doc(keyword = "crate")]
105 //
106 /// A Rust binary or library.
107 ///
108 /// The primary use of the `crate` keyword is as a part of `extern crate` declarations, which are
109 /// used to specify a dependency on a crate external to the one it's declared in. Crates are the
110 /// fundamental compilation unit of Rust code, and can be seen as libraries or projects. More can
111 /// be read about crates in the [Reference].
112 ///
113 /// ```rust ignore
114 /// extern crate rand;
115 /// extern crate my_crate as thing;
116 /// extern crate std; // implicitly added to the root of every Rust project
117 /// ```
118 ///
119 /// The `as` keyword can be used to change what the crate is referred to as in your project. If a
120 /// crate name includes a dash, it is implicitly imported with the dashes replaced by underscores.
121 ///
122 /// `crate` can also be used as in conjunction with `pub` to signify that the item it's attached to
123 /// is public only to other members of the same crate it's in.
124 ///
125 /// ```rust
126 /// # #[allow(unused_imports)]
127 /// pub(crate) use std::io::Error as IoError;
128 /// pub(crate) enum CoolMarkerType { }
129 /// pub struct PublicThing {
130 ///     pub(crate) semi_secret_thing: bool,
131 /// }
132 /// ```
133 ///
134 /// `crate` is also used to represent the absolute path of a module, where `crate` refers to the
135 /// root of the current crate. For instance, `crate::foo::bar` refers to the name `bar` inside the
136 /// module `foo`, from anywhere else in the same crate.
137 ///
138 /// [Reference]: ../reference/items/extern-crates.html
139 mod crate_keyword { }
140
141 #[doc(keyword = "else")]
142 //
143 /// What to do when an [`if`] condition does not hold.
144 ///
145 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
146 ///
147 /// [`if`]: keyword.if.html
148 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
149 mod else_keyword { }
150
151 #[doc(keyword = "enum")]
152 //
153 /// A type that can be any one of several variants.
154 ///
155 /// Enums in Rust are similar to those of other compiled languages like C, but have important
156 /// differences that make them considerably more powerful. What Rust calls enums are more commonly
157 /// known as [Algebraic Data Types][ADT] if you're coming from a functional programming background.
158 /// The important detail is that each enum variant can have data to go along with it.
159 ///
160 /// ```rust
161 /// # struct Coord;
162 /// enum SimpleEnum {
163 ///     FirstVariant,
164 ///     SecondVariant,
165 ///     ThirdVariant,
166 /// }
167 ///
168 /// enum Location {
169 ///     Unknown,
170 ///     Anonymous,
171 ///     Known(Coord),
172 /// }
173 ///
174 /// enum ComplexEnum {
175 ///     Nothing,
176 ///     Something(u32),
177 ///     LotsOfThings {
178 ///         usual_struct_stuff: bool,
179 ///         blah: String,
180 ///     }
181 /// }
182 ///
183 /// enum EmptyEnum { }
184 /// ```
185 ///
186 /// The first enum shown is the usual kind of enum you'd find in a C-style language. The second
187 /// shows off a hypothetical example of something storing location data, with `Coord` being any
188 /// other type that's needed, for example a struct. The third example demonstrates the kind of
189 /// data a variant can store, ranging from nothing, to a tuple, to an anonymous struct.
190 ///
191 /// Instantiating enum variants involves explicitly using the enum's name as its namespace,
192 /// followed by one of its variants. `SimpleEnum::SecondVariant` would be an example from above.
193 /// When data follows along with a variant, such as with rust's built-in [`Option`] type, the data
194 /// is added as the type describes, for example `Option::Some(123)`. The same follows with
195 /// struct-like variants, with things looking like `ComplexEnum::LotsOfThings { usual_struct_stuff:
196 /// true, blah: "hello!".to_string(), }`. Empty Enums are similar to () in that they cannot be
197 /// instantiated at all, and are used mainly to mess with the type system in interesting ways.
198 ///
199 /// For more information, take a look at the [Rust Book] or the [Reference]
200 ///
201 /// [ADT]: https://en.wikipedia.org/wiki/Algebraic_data_type
202 /// [`Option`]: option/enum.Option.html
203 /// [Rust Book]: ../book/ch06-01-defining-an-enum.html
204 /// [Reference]: ../reference/items/enumerations.html
205 mod enum_keyword { }
206
207 #[doc(keyword = "extern")]
208 //
209 /// Link to or import external code.
210 ///
211 /// The `extern` keyword is used in two places in Rust. One is in conjunction with the [`crate`]
212 /// keyword to make your Rust code aware of other Rust crates in your project, i.e., `extern crate
213 /// lazy_static;`. The other use is in foreign function interfaces (FFI).
214 ///
215 /// `extern` is used in two different contexts within FFI. The first is in the form of external
216 /// blocks, for declaring function interfaces that Rust code can call foreign code by.
217 ///
218 /// ```rust ignore
219 /// #[link(name = "my_c_library")]
220 /// extern "C" {
221 ///     fn my_c_function(x: i32) -> bool;
222 /// }
223 /// ```
224 ///
225 /// This code would attempt to link with `libmy_c_library.so` on unix-like systems and
226 /// `my_c_library.dll` on Windows at runtime, and panic if it can't find something to link to. Rust
227 /// code could then use `my_c_function` as if it were any other unsafe Rust function. Working with
228 /// non-Rust languages and FFI is inherently unsafe, so wrappers are usually built around C APIs.
229 ///
230 /// The mirror use case of FFI is also done via the `extern` keyword:
231 ///
232 /// ```rust
233 /// #[no_mangle]
234 /// pub extern fn callable_from_c(x: i32) -> bool {
235 ///     x % 3 == 0
236 /// }
237 /// ```
238 ///
239 /// If compiled as a dylib, the resulting .so could then be linked to from a C library, and the
240 /// function could be used as if it was from any other library.
241 ///
242 /// For more information on FFI, check the [Rust book] or the [Reference].
243 ///
244 /// [Rust book]:
245 /// ../book/ch19-01-unsafe-rust.html#using-extern-functions-to-call-external-code
246 /// [Reference]: ../reference/items/external-blocks.html
247 mod extern_keyword { }
248
249 #[doc(keyword = "false")]
250 //
251 /// A value of type [`bool`] representing logical **false**.
252 ///
253 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
254 ///
255 /// [`bool`]: primitive.bool.html
256 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
257 mod false_keyword { }
258
259 #[doc(keyword = "fn")]
260 //
261 /// A function or function pointer.
262 ///
263 /// Functions are the primary way code is executed within Rust. Function blocks, usually just
264 /// called functions, can be defined in a variety of different places and be assigned many
265 /// different attributes and modifiers.
266 ///
267 /// Standalone functions that just sit within a module not attached to anything else are common,
268 /// but most functions will end up being inside [`impl`] blocks, either on another type itself, or
269 /// as a trait impl for that type.
270 ///
271 /// ```rust
272 /// fn standalone_function() {
273 ///     // code
274 /// }
275 ///
276 /// pub fn public_thing(argument: bool) -> String {
277 ///     // code
278 ///     # "".to_string()
279 /// }
280 ///
281 /// struct Thing {
282 ///     foo: i32,
283 /// }
284 ///
285 /// impl Thing {
286 ///     pub fn new() -> Self {
287 ///         Self {
288 ///             foo: 42,
289 ///         }
290 ///     }
291 /// }
292 /// ```
293 ///
294 /// In addition to presenting fixed types in the form of `fn name(arg: type, ..) -> return_type`,
295 /// functions can also declare a list of type parameters along with trait bounds that they fall
296 /// into.
297 ///
298 /// ```rust
299 /// fn generic_function<T: Clone>(x: T) -> (T, T, T) {
300 ///     (x.clone(), x.clone(), x.clone())
301 /// }
302 ///
303 /// fn generic_where<T>(x: T) -> T
304 ///     where T: std::ops::Add<Output = T> + Copy
305 /// {
306 ///     x + x + x
307 /// }
308 /// ```
309 ///
310 /// Declaring trait bounds in the angle brackets is functionally identical to using a `where`
311 /// clause. It's up to the programmer to decide which works better in each situation, but `where`
312 /// tends to be better when things get longer than one line.
313 ///
314 /// Along with being made public via `pub`, `fn` can also have an [`extern`] added for use in
315 /// FFI.
316 ///
317 /// For more information on the various types of functions and how they're used, consult the [Rust
318 /// book] or the [Reference].
319 ///
320 /// [`impl`]: keyword.impl.html
321 /// [`extern`]: keyword.extern.html
322 /// [Rust book]: ../book/ch03-03-how-functions-work.html
323 /// [Reference]: ../reference/items/functions.html
324 mod fn_keyword { }
325
326 #[doc(keyword = "for")]
327 //
328 /// Iteration with [`in`], trait implementation with [`impl`], or [higher-ranked trait bounds]
329 /// (`for<'a>`).
330 ///
331 /// The `for` keyword is used in many syntactic locations:
332 ///
333 /// * `for` is used in for-in-loops (see below).
334 /// * `for` is used when implementing traits as in `impl Trait for Type` (see [`impl`] for more info
335 ///   on that).
336 /// * `for` is also used for [higher-ranked trait bounds] as in `for<'a> &'a T: PartialEq<i32>`.
337 ///
338 /// for-in-loops, or to be more precise, iterator loops, are a simple syntactic sugar over a common
339 /// practice within Rust, which is to loop over an iterator until that iterator returns `None` (or
340 /// `break` is called).
341 ///
342 /// ```rust
343 /// for i in 0..5 {
344 ///     println!("{}", i * 2);
345 /// }
346 ///
347 /// for i in std::iter::repeat(5) {
348 ///     println!("turns out {} never stops being 5", i);
349 ///     break; // would loop forever otherwise
350 /// }
351 ///
352 /// 'outer: for x in 5..50 {
353 ///     for y in 0..10 {
354 ///         if x == y {
355 ///             break 'outer;
356 ///         }
357 ///     }
358 /// }
359 /// ```
360 ///
361 /// As shown in the example above, `for` loops (along with all other loops) can be tagged, using
362 /// similar syntax to lifetimes (only visually similar, entirely distinct in practice). Giving the
363 /// same tag to `break` breaks the tagged loop, which is useful for inner loops. It is definitely
364 /// not a goto.
365 ///
366 /// A `for` loop expands as shown:
367 ///
368 /// ```rust
369 /// # fn code() { }
370 /// # let iterator = 0..2;
371 /// for loop_variable in iterator {
372 ///     code()
373 /// }
374 /// ```
375 ///
376 /// ```rust
377 /// # fn code() { }
378 /// # let iterator = 0..2;
379 /// {
380 ///     let mut _iter = std::iter::IntoIterator::into_iter(iterator);
381 ///     loop {
382 ///         match _iter.next() {
383 ///             Some(loop_variable) => {
384 ///                 code()
385 ///             },
386 ///             None => break,
387 ///         }
388 ///     }
389 /// }
390 /// ```
391 ///
392 /// More details on the functionality shown can be seen at the [`IntoIterator`] docs.
393 ///
394 /// For more information on for-loops, see the [Rust book] or the [Reference].
395 ///
396 /// [`in`]: keyword.in.html
397 /// [`impl`]: keyword.impl.html
398 /// [higher-ranked trait bounds]: ../reference/trait-bounds.html#higher-ranked-trait-bounds
399 /// [`IntoIterator`]: iter/trait.IntoIterator.html
400 /// [Rust book]:
401 /// ../book/ch03-05-control-flow.html#looping-through-a-collection-with-for
402 /// [Reference]: ../reference/expressions/loop-expr.html#iterator-loops
403 mod for_keyword { }
404
405 #[doc(keyword = "if")]
406 //
407 /// Evaluate a block if a condition holds.
408 ///
409 /// `if` is a familiar construct to most programmers, and is the main way you'll often do logic in
410 /// your code. However, unlike in most languages, `if` blocks can also act as expressions.
411 ///
412 /// ```rust
413 /// # let rude = true;
414 /// if 1 == 2 {
415 ///     println!("whoops, mathematics broke");
416 /// } else {
417 ///     println!("everything's fine!");
418 /// }
419 ///
420 /// let greeting = if rude {
421 ///     "sup nerd."
422 /// } else {
423 ///     "hello, friend!"
424 /// };
425 ///
426 /// if let Ok(x) = "123".parse::<i32>() {
427 ///     println!("{} double that and you get {}!", greeting, x * 2);
428 /// }
429 /// ```
430 ///
431 /// Shown above are the three typical forms an `if` block comes in. First is the usual kind of
432 /// thing you'd see in many languages, with an optional `else` block. Second uses `if` as an
433 /// expression, which is only possible if all branches return the same type. An `if` expression can
434 /// be used everywhere you'd expect. The third kind of `if` block is an `if let` block, which
435 /// behaves similarly to using a `match` expression:
436 ///
437 /// ```rust
438 /// if let Some(x) = Some(123) {
439 ///     // code
440 ///     # let _ = x;
441 /// } else {
442 ///     // something else
443 /// }
444 ///
445 /// match Some(123) {
446 ///     Some(x) => {
447 ///         // code
448 ///         # let _ = x;
449 ///     },
450 ///     _ => {
451 ///         // something else
452 ///     },
453 /// }
454 /// ```
455 ///
456 /// Each kind of `if` expression can be mixed and matched as needed.
457 ///
458 /// ```rust
459 /// if true == false {
460 ///     println!("oh no");
461 /// } else if "something" == "other thing" {
462 ///     println!("oh dear");
463 /// } else if let Some(200) = "blarg".parse::<i32>().ok() {
464 ///     println!("uh oh");
465 /// } else {
466 ///     println!("phew, nothing's broken");
467 /// }
468 /// ```
469 ///
470 /// The `if` keyword is used in one other place in Rust, namely as a part of pattern matching
471 /// itself, allowing patterns such as `Some(x) if x > 200` to be used.
472 ///
473 /// For more information on `if` expressions, see the [Rust book] or the [Reference].
474 ///
475 /// [Rust book]: ../book/ch03-05-control-flow.html#if-expressions
476 /// [Reference]: ../reference/expressions/if-expr.html
477 mod if_keyword { }
478
479 #[doc(keyword = "impl")]
480 //
481 /// Implement some functionality for a type.
482 ///
483 /// The `impl` keyword is primarily used to define implementations on types. Inherent
484 /// implementations are standalone, while trait implementations are used to implement traits for
485 /// types, or other traits.
486 ///
487 /// Functions and consts can both be defined in an implementation. A function defined in an
488 /// `impl` block can be standalone, meaning it would be called like `Foo::bar()`. If the function
489 /// takes `self`, `&self`, or `&mut self` as its first argument, it can also be called using
490 /// method-call syntax, a familiar feature to any object oriented programmer, like `foo.bar()`.
491 ///
492 /// ```rust
493 /// struct Example {
494 ///     number: i32,
495 /// }
496 ///
497 /// impl Example {
498 ///     fn boo() {
499 ///         println!("boo! Example::boo() was called!");
500 ///     }
501 ///
502 ///     fn answer(&mut self) {
503 ///         self.number += 42;
504 ///     }
505 ///
506 ///     fn get_number(&self) -> i32 {
507 ///         self.number
508 ///     }
509 /// }
510 ///
511 /// trait Thingy {
512 ///     fn do_thingy(&self);
513 /// }
514 ///
515 /// impl Thingy for Example {
516 ///     fn do_thingy(&self) {
517 ///         println!("doing a thing! also, number is {}!", self.number);
518 ///     }
519 /// }
520 /// ```
521 ///
522 /// For more information on implementations, see the [Rust book][book1] or the [Reference].
523 ///
524 /// The other use of the `impl` keyword is in `impl Trait` syntax, which can be seen as a shorthand
525 /// for "a concrete type that implements this trait". Its primary use is working with closures,
526 /// which have type definitions generated at compile time that can't be simply typed out.
527 ///
528 /// ```rust
529 /// fn thing_returning_closure() -> impl Fn(i32) -> bool {
530 ///     println!("here's a closure for you!");
531 ///     |x: i32| x % 3 == 0
532 /// }
533 /// ```
534 ///
535 /// For more information on `impl Trait` syntax, see the [Rust book][book2].
536 ///
537 /// [book1]: ../book/ch05-03-method-syntax.html
538 /// [Reference]: ../reference/items/implementations.html
539 /// [book2]: ../book/ch10-02-traits.html#returning-types-that-implement-traits
540 mod impl_keyword { }
541
542 #[doc(keyword = "in")]
543 //
544 /// Iterate over a series of values with [`for`].
545 ///
546 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
547 ///
548 /// [`for`]: keyword.for.html
549 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
550 mod in_keyword { }
551
552 #[doc(keyword = "let")]
553 //
554 /// Bind a value to a variable.
555 ///
556 /// The primary use for the `let` keyword is in `let` statements, which are used to introduce a new
557 /// set of variables into the current scope, as given by a pattern.
558 ///
559 /// ```rust
560 /// # #![allow(unused_assignments)]
561 /// let thing1: i32 = 100;
562 /// let thing2 = 200 + thing1;
563 ///
564 /// let mut changing_thing = true;
565 /// changing_thing = false;
566 ///
567 /// let (part1, part2) = ("first", "second");
568 ///
569 /// struct Example {
570 ///     a: bool,
571 ///     b: u64,
572 /// }
573 ///
574 /// let Example { a, b: _ } = Example {
575 ///     a: true,
576 ///     b: 10004,
577 /// };
578 /// assert!(a);
579 /// ```
580 ///
581 /// The pattern is most commonly a single variable, which means no pattern matching is done and
582 /// the expression given is bound to the variable. Apart from that, patterns used in `let` bindings
583 /// can be as complicated as needed, given that the pattern is exhaustive. See the [Rust
584 /// book][book1] for more information on pattern matching. The type of the pattern is optionally
585 /// given afterwards, but if left blank is automatically inferred by the compiler if possible.
586 ///
587 /// Variables in Rust are immutable by default, and require the `mut` keyword to be made mutable.
588 ///
589 /// Multiple variables can be defined with the same name, known as shadowing. This doesn't affect
590 /// the original variable in any way beyond being unable to directly access it beyond the point of
591 /// shadowing. It continues to remain in scope, getting dropped only when it falls out of scope.
592 /// Shadowed variables don't need to have the same type as the variables shadowing them.
593 ///
594 /// ```rust
595 /// let shadowing_example = true;
596 /// let shadowing_example = 123.4;
597 /// let shadowing_example = shadowing_example as u32;
598 /// let mut shadowing_example = format!("cool! {}", shadowing_example);
599 /// shadowing_example += " something else!"; // not shadowing
600 /// ```
601 ///
602 /// Other places the `let` keyword is used include along with [`if`], in the form of `if let`
603 /// expressions. They're useful if the pattern being matched isn't exhaustive, such as with
604 /// enumerations. `while let` also exists, which runs a loop with a pattern matched value until
605 /// that pattern can't be matched.
606 ///
607 /// For more information on the `let` keyword, see the [Rust book][book2] or the [Reference]
608 ///
609 /// [book1]: ../book/ch06-02-match.html
610 /// [`if`]: keyword.if.html
611 /// [book2]: ../book/ch18-01-all-the-places-for-patterns.html#let-statements
612 /// [Reference]: ../reference/statements.html#let-statements
613 mod let_keyword { }
614
615 #[doc(keyword = "while")]
616 //
617 /// Loop while a condition is upheld.
618 ///
619 /// A `while` expression is used for predicate loops. The `while` expression runs the conditional
620 /// expression before running the loop body, then runs the loop body if the conditional
621 /// expression evaluates to `true`, or exits the loop otherwise.
622 ///
623 /// ```rust
624 /// let mut counter = 0;
625 ///
626 /// while counter < 10 {
627 ///     println!("{}", counter);
628 ///     counter += 1;
629 /// }
630 /// ```
631 ///
632 /// Like the [`for`] expression, we can use `break` and `continue`. A `while` expression
633 /// cannot break with a value and always evaluates to `()` unlike [`loop`].
634 ///
635 /// ```rust
636 /// let mut i = 1;
637 ///
638 /// while i < 100 {
639 ///     i *= 2;
640 ///     if i == 64 {
641 ///         break; // Exit when `i` is 64.
642 ///     }
643 /// }
644 /// ```
645 ///
646 /// As `if` expressions have their pattern matching variant in `if let`, so too do `while`
647 /// expressions with `while let`. The `while let` expression matches the pattern against the
648 /// expression, then runs the loop body if pattern matching succeeds, or exits the loop otherwise.
649 /// We can use `break` and `continue` in `while let` expressions just like in `while`.
650 ///
651 /// ```rust
652 /// let mut counter = Some(0);
653 ///
654 /// while let Some(i) = counter {
655 ///     if i == 10 {
656 ///         counter = None;
657 ///     } else {
658 ///         println!("{}", i);
659 ///         counter = Some (i + 1);
660 ///     }
661 /// }
662 /// ```
663 ///
664 /// For more information on `while` and loops in general, see the [reference].
665 ///
666 /// [`for`]: keyword.for.html
667 /// [`loop`]: keyword.loop.html
668 /// [reference]: ../reference/expressions/loop-expr.html#predicate-loops
669 mod while_keyword { }
670
671 #[doc(keyword = "loop")]
672 //
673 /// Loop indefinitely.
674 ///
675 /// `loop` is used to define the simplest kind of loop supported in Rust. It runs the code inside
676 /// it until the code uses `break` or the program exits.
677 ///
678 /// ```rust
679 /// loop {
680 ///     println!("hello world forever!");
681 ///     # break;
682 /// }
683 ///
684 /// let mut i = 1;
685 /// loop {
686 ///     println!("i is {}", i);
687 ///     if i > 100 {
688 ///         break;
689 ///     }
690 ///     i *= 2;
691 /// }
692 /// assert_eq!(i, 128);
693 /// ```
694 ///
695 /// Unlike the other kinds of loops in Rust (`while`, `while let`, and `for`), loops can be used as
696 /// expressions that return values via `break`.
697 ///
698 /// ```rust
699 /// let mut i = 1;
700 /// let something = loop {
701 ///     i *= 2;
702 ///     if i > 100 {
703 ///         break i;
704 ///     }
705 /// };
706 /// assert_eq!(something, 128);
707 /// ```
708 ///
709 /// Every `break` in a loop has to have the same type. When it's not explicitly giving something,
710 /// `break;` returns `()`.
711 ///
712 /// For more information on `loop` and loops in general, see the [Reference].
713 ///
714 /// [Reference]: ../reference/expressions/loop-expr.html
715 mod loop_keyword { }
716
717 #[doc(keyword = "match")]
718 //
719 /// Control flow based on pattern matching.
720 ///
721 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
722 ///
723 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
724 mod match_keyword { }
725
726 #[doc(keyword = "mod")]
727 //
728 /// Organize code into [modules].
729 ///
730 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
731 ///
732 /// [modules]: ../reference/items/modules.html
733 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
734 mod mod_keyword { }
735
736 #[doc(keyword = "move")]
737 //
738 /// Capture a [closure]'s environment by value.
739 ///
740 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
741 ///
742 /// [closure]: ../book/second-edition/ch13-01-closures.html
743 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
744 mod move_keyword { }
745
746 #[doc(keyword = "mut")]
747 //
748 /// A mutable binding, reference, or pointer.
749 ///
750 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
751 ///
752 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
753 mod mut_keyword { }
754
755 #[doc(keyword = "pub")]
756 //
757 /// Make an item visible to others.
758 ///
759 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
760 ///
761 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
762 mod pub_keyword { }
763
764 #[doc(keyword = "ref")]
765 //
766 /// Bind by reference during pattern matching.
767 ///
768 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
769 ///
770 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
771 mod ref_keyword { }
772
773 #[doc(keyword = "return")]
774 //
775 /// Return a value from a function.
776 ///
777 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
778 ///
779 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
780 mod return_keyword { }
781
782 #[doc(keyword = "self")]
783 //
784 /// The receiver of a method, or the current module.
785 ///
786 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
787 ///
788 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
789 mod self_keyword { }
790
791 #[doc(keyword = "Self")]
792 //
793 /// The implementing type within a [`trait`] or [`impl`] block, or the current type within a type
794 /// definition.
795 ///
796 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
797 ///
798 /// [`impl`]: keyword.impl.html
799 /// [`trait`]: keyword.trait.html
800 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
801 mod self_upper_keyword { }
802
803 #[doc(keyword = "static")]
804 //
805 /// A place that is valid for the duration of a program.
806 ///
807 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
808 ///
809 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
810 mod static_keyword { }
811
812 #[doc(keyword = "struct")]
813 //
814 /// A type that is composed of other types.
815 ///
816 /// Structs in Rust come in three flavors: Structs with named fields, tuple structs, and unit
817 /// structs.
818 ///
819 /// ```rust
820 /// struct Regular {
821 ///     field1: f32,
822 ///     field2: String,
823 ///     pub field3: bool
824 /// }
825 ///
826 /// struct Tuple(u32, String);
827 ///
828 /// struct Unit;
829 /// ```
830 ///
831 /// Regular structs are the most commonly used. Each field defined within them has a name and a
832 /// type, and once defined can be accessed using `example_struct.field` syntax. The fields of a
833 /// struct share its mutability, so `foo.bar = 2;` would only be valid if `foo` was mutable. Adding
834 /// `pub` to a field makes it visible to code in other modules, as well as allowing it to be
835 /// directly accessed and modified.
836 ///
837 /// Tuple structs are similar to regular structs, but its fields have no names. They are used like
838 /// tuples, with deconstruction possible via `let TupleStruct(x, y) = foo;` syntax. For accessing
839 /// individual variables, the same syntax is used as with regular tuples, namely `foo.0`, `foo.1`,
840 /// etc, starting at zero.
841 ///
842 /// Unit structs are most commonly used as marker. They have a size of zero bytes, but unlike empty
843 /// enums they can be instantiated, making them isomorphic to the unit type `()`. Unit structs are
844 /// useful when you need to implement a trait on something, but don't need to store any data inside
845 /// it.
846 ///
847 /// # Instantiation
848 ///
849 /// Structs can be instantiated in different ways, all of which can be mixed and
850 /// matched as needed. The most common way to make a new struct is via a constructor method such as
851 /// `new()`, but when that isn't available (or you're writing the constructor itself), struct
852 /// literal syntax is used:
853 ///
854 /// ```rust
855 /// # struct Foo { field1: f32, field2: String, etc: bool }
856 /// let example = Foo {
857 ///     field1: 42.0,
858 ///     field2: "blah".to_string(),
859 ///     etc: true,
860 /// };
861 /// ```
862 ///
863 /// It's only possible to directly instantiate a struct using struct literal syntax when all of its
864 /// fields are visible to you.
865 ///
866 /// There are a handful of shortcuts provided to make writing constructors more convenient, most
867 /// common of which is the Field Init shorthand. When there is a variable and a field of the same
868 /// name, the assignment can be simplified from `field: field` into simply `field`. The following
869 /// example of a hypothetical constructor demonstrates this:
870 ///
871 /// ```rust
872 /// struct User {
873 ///     name: String,
874 ///     admin: bool,
875 /// }
876 ///
877 /// impl User {
878 ///     pub fn new(name: String) -> Self {
879 ///         Self {
880 ///             name,
881 ///             admin: false,
882 ///         }
883 ///     }
884 /// }
885 /// ```
886 ///
887 /// Another shortcut for struct instantiation is available, used when you need to make a new
888 /// struct that has the same values as most of a previous struct of the same type, called struct
889 /// update syntax:
890 ///
891 /// ```rust
892 /// # struct Foo { field1: String, field2: () }
893 /// # let thing = Foo { field1: "".to_string(), field2: () };
894 /// let updated_thing = Foo {
895 ///     field1: "a new value".to_string(),
896 ///     ..thing
897 /// };
898 /// ```
899 ///
900 /// Tuple structs are instantiated in the same way as tuples themselves, except with the struct's
901 /// name as a prefix: `Foo(123, false, 0.1)`.
902 ///
903 /// Empty structs are instantiated with just their name, and don't need anything else. `let thing =
904 /// EmptyStruct;`
905 ///
906 /// # Style conventions
907 ///
908 /// Structs are always written in CamelCase, with few exceptions. While the trailing comma on a
909 /// struct's list of fields can be omitted, it's usually kept for convenience in adding and
910 /// removing fields down the line.
911 ///
912 /// For more information on structs, take a look at the [Rust Book][book] or the
913 /// [Reference][reference].
914 ///
915 /// [`PhantomData`]: marker/struct.PhantomData.html
916 /// [book]: ../book/ch05-01-defining-structs.html
917 /// [reference]: ../reference/items/structs.html
918 mod struct_keyword { }
919
920 #[doc(keyword = "super")]
921 //
922 /// The parent of the current [module].
923 ///
924 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
925 ///
926 /// [module]: ../reference/items/modules.html
927 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
928 mod super_keyword { }
929
930 #[doc(keyword = "trait")]
931 //
932 /// A common interface for a class of types.
933 ///
934 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
935 ///
936 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
937 mod trait_keyword { }
938
939 #[doc(keyword = "true")]
940 //
941 /// A value of type [`bool`] representing logical **true**.
942 ///
943 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
944 ///
945 /// [`bool`]: primitive.bool.html
946 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
947 mod true_keyword { }
948
949 #[doc(keyword = "type")]
950 //
951 /// Define an alias for an existing type.
952 ///
953 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
954 ///
955 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
956 mod type_keyword { }
957
958 #[doc(keyword = "unsafe")]
959 //
960 /// Code or interfaces whose [memory safety] cannot be verified by the type system.
961 ///
962 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
963 ///
964 /// [memory safety]: ../book/ch19-01-unsafe-rust.html
965 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
966 mod unsafe_keyword { }
967
968 #[doc(keyword = "use")]
969 //
970 /// Import or rename items from other crates or modules.
971 ///
972 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
973 ///
974 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
975 mod use_keyword { }
976
977 #[doc(keyword = "where")]
978 //
979 /// Add constraints that must be upheld to use an item.
980 ///
981 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
982 ///
983 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
984 mod where_keyword { }
985
986 // 2018 Edition keywords
987
988 #[doc(keyword = "async")]
989 //
990 /// Return a [`Future`] instead of blocking the current thread.
991 ///
992 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
993 ///
994 /// [`Future`]: ./future/trait.Future.html
995 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
996 mod async_keyword { }
997
998 #[doc(keyword = "await")]
999 //
1000 /// Suspend execution until the result of a [`Future`] is ready.
1001 ///
1002 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1003 ///
1004 /// [`Future`]: ./future/trait.Future.html
1005 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1006 mod await_keyword { }
1007
1008 #[doc(keyword = "dyn")]
1009 //
1010 /// Name the type of a [trait object].
1011 ///
1012 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1013 ///
1014 /// [trait object]: ../book/ch17-02-trait-objects.html
1015 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1016 mod dyn_keyword { }
1017
1018 #[doc(keyword = "union")]
1019 //
1020 /// The [Rust equivalent of a C-style union][union].
1021 ///
1022 /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1023 ///
1024 /// [union]: ../reference/items/unions.html
1025 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1026 mod union_keyword { }