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