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