]> git.lizzy.rs Git - rust.git/blob - src/libstd/keyword_docs.rs
94751cec41618a31fe363134f19c4a4428b8643d
[rust.git] / src / libstd / keyword_docs.rs
1 #[doc(keyword = "as")]
2 //
3 /// The keyword for casting a value to a type.
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]:
29 /// https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
30 /// [`crate`]: keyword.crate.html
31 mod as_keyword { }
32
33 #[doc(keyword = "const")]
34 //
35 /// The keyword for defining constants.
36 ///
37 /// Sometimes a certain value is used many times throughout a program, and it can become
38 /// inconvenient to copy it over and over. What's more, it's not always possible or desirable to
39 /// make it a variable that gets carried around to each function that needs it. In these cases, the
40 /// `const` keyword provides a convenient alternative to code duplication.
41 ///
42 /// ```rust
43 /// const THING: u32 = 0xABAD1DEA;
44 ///
45 /// let foo = 123 + THING;
46 /// ```
47 ///
48 /// Constants must be explicitly typed, unlike with `let` you can't ignore its type and let the
49 /// compiler figure it out. Any constant value can be defined in a const, which in practice happens
50 /// to be most things that would be reasonable to have a constant (barring `const fn`s). For
51 /// example, you can't have a File as a `const`.
52 ///
53 /// The only lifetime allowed in a constant is `'static`, which is the lifetime that encompasses
54 /// all others in a Rust program. For example, if you wanted to define a constant string, it would
55 /// look like this:
56 ///
57 /// ```rust
58 /// const WORDS: &str = "hello rust!";
59 /// ```
60 ///
61 /// Thanks to static lifetime elision, you usually don't have to explicitly use 'static:
62 ///
63 /// ```rust
64 /// const WORDS: &str = "hello convenience!";
65 /// ```
66 ///
67 /// `const` items looks remarkably similar to `static` items, which introduces some confusion as
68 /// to which one should be used at which times. To put it simply, constants are inlined wherever
69 /// they're used, making using them identical to simply replacing the name of the const with its
70 /// value. Static variables on the other hand point to a single location in memory, which all
71 /// accesses share. This means that, unlike with constants, they can't have destructors, and act as
72 /// a single value across the entire codebase.
73 ///
74 /// Constants, as with statics, should always be in SCREAMING_SNAKE_CASE.
75 ///
76 /// The `const` keyword is also used in raw pointers in combination with `mut`, as seen in `*const
77 /// T` and `*mut T`. More about that can be read at the [pointer] primitive part of the Rust docs.
78 ///
79 /// For more detail on `const`, see the [Rust Book] or the [Reference]
80 ///
81 /// [pointer]: primitive.pointer.html
82 /// [Rust Book]:
83 /// https://doc.rust-lang.org/stable/book/2018-edition/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants
84 /// [Reference]: https://doc.rust-lang.org/reference/items/constant-items.html
85 mod const_keyword { }
86
87 #[doc(keyword = "crate")]
88 //
89 /// The `crate` keyword.
90 ///
91 /// The primary use of the `crate` keyword is as a part of `extern crate` declarations, which are
92 /// used to specify a dependency on a crate external to the one it's declared in. Crates are the
93 /// fundamental compilation unit of Rust code, and can be seen as libraries or projects. More can
94 /// be read about crates in the [Reference].
95 ///
96 /// ```rust ignore
97 /// extern crate rand;
98 /// extern crate my_crate as thing;
99 /// extern crate std; // implicitly added to the root of every Rust project
100 /// ```
101 ///
102 /// The `as` keyword can be used to change what the crate is referred to as in your project. If a
103 /// crate name includes a dash, it is implicitly imported with the dashes replaced by underscores.
104 ///
105 /// `crate` is also used as in conjunction with `pub` to signify that the item it's attached to
106 /// is public only to other members of the same crate it's in.
107 ///
108 /// ```rust
109 /// # #[allow(unused_imports)]
110 /// pub(crate) use std::io::Error as IoError;
111 /// pub(crate) enum CoolMarkerType { }
112 /// pub struct PublicThing {
113 ///     pub(crate) semi_secret_thing: bool,
114 /// }
115 /// ```
116 ///
117 /// [Reference]: https://doc.rust-lang.org/reference/items/extern-crates.html
118 mod crate_keyword { }
119
120 #[doc(keyword = "enum")]
121 //
122 /// For defining enumerations.
123 ///
124 /// Enums in Rust are similar to those of other compiled languages like C, but have important
125 /// differences that make them considerably more powerful. What Rust calls enums are more commonly
126 /// known as [Algebraic Data Types] if you're coming from a functional programming background. The
127 /// important detail is that each enum variant can have data to go along with it.
128 ///
129 /// ```rust
130 /// # struct Coord;
131 /// enum SimpleEnum {
132 ///     FirstVariant,
133 ///     SecondVariant,
134 ///     ThirdVariant,
135 /// }
136 ///
137 /// enum Location {
138 ///     Unknown,
139 ///     Anonymous,
140 ///     Known(Coord),
141 /// }
142 ///
143 /// enum ComplexEnum {
144 ///     Nothing,
145 ///     Something(u32),
146 ///     LotsOfThings {
147 ///         usual_struct_stuff: bool,
148 ///         blah: String,
149 ///     }
150 /// }
151 ///
152 /// enum EmptyEnum { }
153 /// ```
154 ///
155 /// The first enum shown is the usual kind of enum you'd find in a C-style language. The second
156 /// shows off a hypothetical example of something storing location data, with `Coord` being any
157 /// other type that's needed, for example a struct. The third example demonstrates the kind of
158 /// data a variant can store, ranging from nothing, to a tuple, to an anonymous struct.
159 ///
160 /// Instantiating enum variants involves explicitly using the enum's name as its namespace,
161 /// followed by one of its variants. `SimpleEnum::SecondVariant` would be an example from above.
162 /// When data follows along with a variant, such as with rust's built-in [`Option`] type, the data
163 /// is added as the type describes, for example `Option::Some(123)`. The same follows with
164 /// struct-like variants, with things looking like `ComplexEnum::LotsOfThings { usual_struct_stuff:
165 /// true, blah: "hello!".to_string(), }`. Empty Enums are similar to () in that they cannot be
166 /// instantiated at all, and are used mainly to mess with the type system in interesting ways.
167 ///
168 /// For more information, take a look at the [Rust Book] or the [Reference]
169 ///
170 /// [Algebraic Data Types]: https://en.wikipedia.org/wiki/Algebraic_data_type
171 /// [`Option`]: option/enum.Option.html
172 /// [Rust Book]: https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html
173 /// [Reference]: https://doc.rust-lang.org/reference/items/enumerations.html
174 mod enum_keyword { }
175
176 #[doc(keyword = "extern")]
177 //
178 /// For external connections in Rust code.
179 ///
180 /// The `extern` keyword is used in two places in Rust. One is in conjunction with the [`crate`]
181 /// keyword to make your Rust code aware of other Rust crates in your project, i.e., `extern crate
182 /// lazy_static;`. The other use is in foreign function interfaces (FFI).
183 ///
184 /// `extern` is used in two different contexts within FFI. The first is in the form of external
185 /// blocks, for declaring function interfaces that Rust code can call foreign code by.
186 ///
187 /// ```rust ignore
188 /// #[link(name = "my_c_library")]
189 /// extern "C" {
190 ///     fn my_c_function(x: i32) -> bool;
191 /// }
192 /// ```
193 ///
194 /// This code would attempt to link with `libmy_c_library.so` on unix-like systems and
195 /// `my_c_library.dll` on Windows at runtime, and panic if it can't find something to link to. Rust
196 /// code could then use `my_c_function` as if it were any other unsafe Rust function. Working with
197 /// non-Rust languages and FFI is inherently unsafe, so wrappers are usually built around C APIs.
198 ///
199 /// The mirror use case of FFI is also done via the `extern` keyword:
200 ///
201 /// ```rust
202 /// #[no_mangle]
203 /// pub extern fn callable_from_c(x: i32) -> bool {
204 ///     x % 3 == 0
205 /// }
206 /// ```
207 ///
208 /// If compiled as a dylib, the resulting .so could then be linked to from a C library, and the
209 /// function could be used as if it was from any other library.
210 ///
211 /// For more information on FFI, check the [Rust book] or the [Reference].
212 ///
213 /// [Rust book]:
214 /// https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html#using-extern-functions-to-call-external-code
215 /// [Reference]: https://doc.rust-lang.org/reference/items/external-blocks.html
216 mod extern_keyword { }
217
218 #[doc(keyword = "fn")]
219 //
220 /// The keyword for defining functions.
221 ///
222 /// Functions are the primary way code is executed within Rust. Function blocks, usually just
223 /// called functions, can be defined in a variety of different places and be assigned many
224 /// different attributes and modifiers.
225 ///
226 /// Standalone functions that just sit within a module not attached to anything else are common,
227 /// but most functions will end up being inside [`impl`] blocks, either on another type itself, or
228 /// as a trait impl for that type.
229 ///
230 /// ```rust
231 /// fn standalone_function() {
232 ///     // code
233 /// }
234 ///
235 /// pub fn public_thing(argument: bool) -> String {
236 ///     // code
237 ///     # "".to_string()
238 /// }
239 ///
240 /// struct Thing {
241 ///     foo: i32,
242 /// }
243 ///
244 /// impl Thing {
245 ///     pub fn new() -> Self {
246 ///         Self {
247 ///             foo: 42,
248 ///         }
249 ///     }
250 /// }
251 /// ```
252 ///
253 /// In addition to presenting fixed types in the form of `fn name(arg: type, ..) -> return_type`,
254 /// functions can also declare a list of type parameters along with trait bounds that they fall
255 /// into.
256 ///
257 /// ```rust
258 /// fn generic_function<T: Clone>(x: T) -> (T, T, T) {
259 ///     (x.clone(), x.clone(), x.clone())
260 /// }
261 ///
262 /// fn generic_where<T>(x: T) -> T
263 ///     where T: std::ops::Add<Output = T> + Copy
264 /// {
265 ///     x + x + x
266 /// }
267 /// ```
268 ///
269 /// Declaring trait bounds in the angle brackets is functionally identical to using a `where`
270 /// clause. It's up to the programmer to decide which works better in each situation, but `where`
271 /// tends to be better when things get longer than one line.
272 ///
273 /// Along with being made public via `pub`, `fn` can also have an [`extern`] added for use in
274 /// FFI.
275 ///
276 /// For more information on the various types of functions and how they're used, consult the [Rust
277 /// book] or the [Reference].
278 ///
279 /// [`impl`]: keyword.impl.html
280 /// [`extern`]: keyword.extern.html
281 /// [Rust book]: https://doc.rust-lang.org/book/ch03-03-how-functions-work.html
282 /// [Reference]: https://doc.rust-lang.org/reference/items/functions.html
283 mod fn_keyword { }
284
285 #[doc(keyword = "for")]
286 //
287 /// The `for` keyword.
288 ///
289 /// `for` is primarily used in for-in-loops, but it has a few other pieces of syntactic
290 /// uses.  `for` is used when implementing traits as in `impl Trait for Type` (see
291 /// [`impl`] for more info on that).  `for` is also used for [higher-ranked trait bounds]
292 /// as in `for<'a> &'a T: PartialEq<i32>`.  for-in-loops, or to be more precise, iterator
293 /// loops, are a simple syntactic sugar over an exceedingly common practice within Rust,
294 /// which is to loop over an iterator until that iterator returns `None` (or `break` is
295 /// called).
296 ///
297 /// ```rust
298 /// for i in 0..5 {
299 ///     println!("{}", i * 2);
300 /// }
301 ///
302 /// for i in std::iter::repeat(5) {
303 ///     println!("turns out {} never stops being 5", i);
304 ///     break; // would loop forever otherwise
305 /// }
306 ///
307 /// 'outer: for x in 5..50 {
308 ///     for y in 0..10 {
309 ///         if x == y {
310 ///             break 'outer;
311 ///         }
312 ///     }
313 /// }
314 /// ```
315 ///
316 /// As shown in the example above, `for` loops (along with all other loops) can be tagged, using
317 /// similar syntax to lifetimes (only visually similar, entirely distinct in practice). Giving the
318 /// same tag to `break` breaks the tagged loop, which is useful for inner loops. It is definitely
319 /// not a goto.
320 ///
321 /// A `for` loop expands as shown:
322 ///
323 /// ```rust
324 /// # fn code() { }
325 /// # let iterator = 0..2;
326 /// for loop_variable in iterator {
327 ///     code()
328 /// }
329 /// ```
330 ///
331 /// ```rust
332 /// # fn code() { }
333 /// # let iterator = 0..2;
334 /// {
335 ///     let mut _iter = std::iter::IntoIterator::into_iter(iterator);
336 ///     loop {
337 ///         match _iter.next() {
338 ///             Some(loop_variable) => {
339 ///                 code()
340 ///             },
341 ///             None => break,
342 ///         }
343 ///     }
344 /// }
345 /// ```
346 ///
347 /// More details on the functionality shown can be seen at the [`IntoIterator`] docs.
348 ///
349 /// For more information on for-loops, see the [Rust book] or the [Reference].
350 ///
351 /// [`impl`]: keyword.impl.html
352 /// [`IntoIterator`]: iter/trait.IntoIterator.html
353 /// [Rust book]:
354 /// https://doc.rust-lang.org/book/2018-edition/ch03-05-control-flow.html#looping-through-a-collection-with-for
355 /// [Reference]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#iterator-loops
356 mod for_keyword { }
357
358 #[doc(keyword = "if")]
359 //
360 /// If statements and expressions.
361 ///
362 /// `if` is a familiar construct to most programmers, and is the main way you'll often do logic in
363 /// your code. However, unlike in most languages, `if` blocks can also act as expressions.
364 ///
365 /// ```rust
366 /// # let rude = true;
367 /// if 1 == 2 {
368 ///     println!("whoops, mathematics broke");
369 /// } else {
370 ///     println!("everything's fine!");
371 /// }
372 ///
373 /// let greeting = if rude {
374 ///     "sup nerd."
375 /// } else {
376 ///     "hello, friend!"
377 /// };
378 ///
379 /// if let Ok(x) = "123".parse::<i32>() {
380 ///     println!("{} double that and you get {}!", greeting, x * 2);
381 /// }
382 /// ```
383 ///
384 /// Shown above are the three typical forms an `if` block comes in. First is the usual kind of
385 /// thing you'd see in many languages, with an optional `else` block. Second uses `if` as an
386 /// expression, which is only possible if all branches return the same type. An `if` expression can
387 /// be used everywhere you'd expect. The third kind of `if` block is an `if let` block, which
388 /// behaves similarly to using a `match` expression:
389 ///
390 /// ```rust
391 /// if let Some(x) = Some(123) {
392 ///     // code
393 ///     # let _ = x;
394 /// } else {
395 ///     // something else
396 /// }
397 ///
398 /// match Some(123) {
399 ///     Some(x) => {
400 ///         // code
401 ///         # let _ = x;
402 ///     },
403 ///     _ => {
404 ///         // something else
405 ///     },
406 /// }
407 /// ```
408 ///
409 /// Each kind of `if` expression can be mixed and matched as needed.
410 ///
411 /// ```rust
412 /// if true == false {
413 ///     println!("oh no");
414 /// } else if "something" == "other thing" {
415 ///     println!("oh dear");
416 /// } else if let Some(200) = "blarg".parse::<i32>().ok() {
417 ///     println!("uh oh");
418 /// } else {
419 ///     println!("phew, nothing's broken");
420 /// }
421 /// ```
422 ///
423 /// The `if` keyword is used in one other place in Rust, namely as a part of pattern matching
424 /// itself, allowing patterns such as `Some(x) if x > 200` to be used.
425 ///
426 /// For more information on `if` expressions, see the [Rust book] or the [Reference].
427 ///
428 /// [Rust book]:
429 /// https://doc.rust-lang.org/stable/book/2018-edition/ch03-05-control-flow.html#if-expressions
430 /// [Reference]: https://doc.rust-lang.org/reference/expressions/if-expr.html
431 mod if_keyword { }
432
433 #[doc(keyword = "impl")]
434 //
435 /// The implementation-defining keyword.
436 ///
437 /// The `impl` keyword is primarily used to define implementations on types. Inherent
438 /// implementations are standalone, while trait implementations are used to implement traits for
439 /// types, or other traits.
440 ///
441 /// Functions and consts can both be defined in an implementation. A function defined in an
442 /// `impl` block can be standalone, meaning it would be called like `Foo::bar()`. If the function
443 /// takes `self`, `&self`, or `&mut self` as its first argument, it can also be called using
444 /// method-call syntax, a familiar feature to any object oriented programmer, like `foo.bar()`.
445 ///
446 /// ```rust
447 /// struct Example {
448 ///     number: i32,
449 /// }
450 ///
451 /// impl Example {
452 ///     fn boo() {
453 ///         println!("boo! Example::boo() was called!");
454 ///     }
455 ///
456 ///     fn answer(&mut self) {
457 ///         self.number += 42;
458 ///     }
459 ///
460 ///     fn get_number(&self) -> i32 {
461 ///         self.number
462 ///     }
463 /// }
464 ///
465 /// trait Thingy {
466 ///     fn do_thingy(&self);
467 /// }
468 ///
469 /// impl Thingy for Example {
470 ///     fn do_thingy(&self) {
471 ///         println!("doing a thing! also, number is {}!", self.number);
472 ///     }
473 /// }
474 /// ```
475 ///
476 /// For more information on implementations, see the [Rust book][book1] or the [Reference].
477 ///
478 /// The other use of the `impl` keyword is in `impl Trait` syntax, which can be seen as a shorthand
479 /// for "a concrete type that implements this trait". Its primary use is working with closures,
480 /// which have type definitions generated at compile time that can't be simply typed out.
481 ///
482 /// ```rust
483 /// fn thing_returning_closure() -> impl Fn(i32) -> bool {
484 ///     println!("here's a closure for you!");
485 ///     |x: i32| x % 3 == 0
486 /// }
487 /// ```
488 ///
489 /// For more information on `impl Trait` syntax, see the [Rust book][book2].
490 ///
491 /// [book1]: https://doc.rust-lang.org/stable/book/2018-edition/ch05-03-method-syntax.html
492 /// [Reference]: https://doc.rust-lang.org/reference/items/implementations.html
493 /// [book2]:
494 /// https://doc.rust-lang.org/stable/book/2018-edition/ch10-02-traits.html#returning-traits
495 mod impl_keyword { }
496
497 #[doc(keyword = "let")]
498 //
499 /// The variable binding keyword.
500 ///
501 /// The primary use for the `let` keyword is in `let` statements, which are used to introduce a new
502 /// set of variables into the current scope, as given by a pattern.
503 ///
504 /// ```rust
505 /// # #![allow(unused_assignments)]
506 /// let thing1: i32 = 100;
507 /// let thing2 = 200 + thing1;
508 ///
509 /// let mut changing_thing = true;
510 /// changing_thing = false;
511 ///
512 /// let (part1, part2) = ("first", "second");
513 ///
514 /// struct Example {
515 ///     a: bool,
516 ///     b: u64,
517 /// }
518 ///
519 /// let Example { a, b: _ } = Example {
520 ///     a: true,
521 ///     b: 10004,
522 /// };
523 /// assert!(a);
524 /// ```
525 ///
526 /// The pattern is most commonly a single variable, which means no pattern matching is done and
527 /// the expression given is bound to the variable. Apart from that, patterns used in `let` bindings
528 /// can be as complicated as needed, given that the pattern is exhaustive. See the [Rust
529 /// book][book1] for more information on pattern matching. The type of the pattern is optionally
530 /// given afterwards, but if left blank is automatically inferred by the compiler if possible.
531 ///
532 /// Variables in Rust are immutable by default, and require the `mut` keyword to be made mutable.
533 ///
534 /// Multiple variables can be defined with the same name, known as shadowing. This doesn't affect
535 /// the original variable in any way beyond being unable to directly access it beyond the point of
536 /// shadowing. It continues to remain in scope, getting dropped only when it falls out of scope.
537 /// Shadowed variables don't need to have the same type as the variables shadowing them.
538 ///
539 /// ```rust
540 /// let shadowing_example = true;
541 /// let shadowing_example = 123.4;
542 /// let shadowing_example = shadowing_example as u32;
543 /// let mut shadowing_example = format!("cool! {}", shadowing_example);
544 /// shadowing_example += " something else!"; // not shadowing
545 /// ```
546 ///
547 /// Other places the `let` keyword is used include along with [`if`], in the form of `if let`
548 /// expressions. They're useful if the pattern being matched isn't exhaustive, such as with
549 /// enumerations. `while let` also exists, which runs a loop with a pattern matched value until
550 /// that pattern can't be matched.
551 ///
552 /// For more information on the `let` keyword, see the [Rust book] or the [Reference]
553 ///
554 /// [book1]: https://doc.rust-lang.org/stable/book/2018-edition/ch06-02-match.html
555 /// [`if`]: keyword.if.html
556 /// [book2]:
557 /// https://doc.rust-lang.org/stable/book/2018-edition/ch18-01-all-the-places-for-patterns.html#let-statements
558 /// [Reference]: https://doc.rust-lang.org/reference/statements.html#let-statements
559 mod let_keyword { }
560
561 #[doc(keyword = "loop")]
562 //
563 /// The loop-defining keyword.
564 ///
565 /// `loop` is used to define the simplest kind of loop supported in Rust. It runs the code inside
566 /// it until the code uses `break` or the program exits.
567 ///
568 /// ```rust
569 /// loop {
570 ///     println!("hello world forever!");
571 ///     # break;
572 /// }
573 ///
574 /// let mut i = 0;
575 /// loop {
576 ///     println!("i is {}", i);
577 ///     if i > 10 {
578 ///         break;
579 ///     }
580 ///     i += 1;
581 /// }
582 /// ```
583 ///
584 /// Unlike the other kinds of loops in Rust (`while`, `while let`, and `for`), loops can be used as
585 /// expressions that return values via `break`.
586 ///
587 /// ```rust
588 /// let mut i = 1;
589 /// let something = loop {
590 ///     i *= 2;
591 ///     if i > 100 {
592 ///         break i;
593 ///     }
594 /// };
595 /// assert_eq!(something, 128);
596 /// ```
597 ///
598 /// Every `break` in a loop has to have the same type. When it's not explicitly giving something,
599 /// `break;` returns `()`.
600 ///
601 /// For more information on `loop` and loops in general, see the [Reference].
602 ///
603 /// [Reference]: https://doc.rust-lang.org/reference/expressions/loop-expr.html
604 mod loop_keyword { }
605
606 #[doc(keyword = "struct")]
607 //
608 /// The keyword used to define structs.
609 ///
610 /// Structs in Rust come in three flavors: Structs with named fields, tuple structs, and unit
611 /// structs.
612 ///
613 /// ```rust
614 /// struct Regular {
615 ///     field1: f32,
616 ///     field2: String,
617 ///     pub field3: bool
618 /// }
619 ///
620 /// struct Tuple(u32, String);
621 ///
622 /// struct Unit;
623 /// ```
624 ///
625 /// Regular structs are the most commonly used. Each field defined within them has a name and a
626 /// type, and once defined can be accessed using `example_struct.field` syntax. The fields of a
627 /// struct share its mutability, so `foo.bar = 2;` would only be valid if `foo` was mutable. Adding
628 /// `pub` to a field makes it visible to code in other modules, as well as allowing it to be
629 /// directly accessed and modified.
630 ///
631 /// Tuple structs are similar to regular structs, but its fields have no names. They are used like
632 /// tuples, with deconstruction possible via `let TupleStruct(x, y) = foo;` syntax. For accessing
633 /// individual variables, the same syntax is used as with regular tuples, namely `foo.0`, `foo.1`,
634 /// etc, starting at zero.
635 ///
636 /// Unit structs are most commonly used as marker. They have a size of zero bytes, but unlike empty
637 /// enums they can be instantiated, making them isomorphic to the unit type `()`. Unit structs are
638 /// useful when you need to implement a trait on something, but don't need to store any data inside
639 /// it.
640 ///
641 /// # Instantiation
642 ///
643 /// Structs can be instantiated in different ways, all of which can be mixed and
644 /// matched as needed. The most common way to make a new struct is via a constructor method such as
645 /// `new()`, but when that isn't available (or you're writing the constructor itself), struct
646 /// literal syntax is used:
647 ///
648 /// ```rust
649 /// # struct Foo { field1: f32, field2: String, etc: bool }
650 /// let example = Foo {
651 ///     field1: 42.0,
652 ///     field2: "blah".to_string(),
653 ///     etc: true,
654 /// };
655 /// ```
656 ///
657 /// It's only possible to directly instantiate a struct using struct literal syntax when all of its
658 /// fields are visible to you.
659 ///
660 /// There are a handful of shortcuts provided to make writing constructors more convenient, most
661 /// common of which is the Field Init shorthand. When there is a variable and a field of the same
662 /// name, the assignment can be simplified from `field: field` into simply `field`. The following
663 /// example of a hypothetical constructor demonstrates this:
664 ///
665 /// ```rust
666 /// struct User {
667 ///     name: String,
668 ///     admin: bool,
669 /// }
670 ///
671 /// impl User {
672 ///     pub fn new(name: String) -> Self {
673 ///         Self {
674 ///             name,
675 ///             admin: false,
676 ///         }
677 ///     }
678 /// }
679 /// ```
680 ///
681 /// Another shortcut for struct instantiation is available, used when you need to make a new
682 /// struct that has the same values as most of a previous struct of the same type, called struct
683 /// update syntax:
684 ///
685 /// ```rust
686 /// # struct Foo { field1: String, field2: () }
687 /// # let thing = Foo { field1: "".to_string(), field2: () };
688 /// let updated_thing = Foo {
689 ///     field1: "a new value".to_string(),
690 ///     ..thing
691 /// };
692 /// ```
693 ///
694 /// Tuple structs are instantiated in the same way as tuples themselves, except with the struct's
695 /// name as a prefix: `Foo(123, false, 0.1)`.
696 ///
697 /// Empty structs are instantiated with just their name, and don't need anything else. `let thing =
698 /// EmptyStruct;`
699 ///
700 /// # Style conventions
701 ///
702 /// Structs are always written in CamelCase, with few exceptions. While the trailing comma on a
703 /// struct's list of fields can be omitted, it's usually kept for convenience in adding and
704 /// removing fields down the line.
705 ///
706 /// For more information on structs, take a look at the [Rust Book][book] or the
707 /// [Reference][reference].
708 ///
709 /// [`PhantomData`]: marker/struct.PhantomData.html
710 /// [book]: https://doc.rust-lang.org/book/ch05-01-defining-structs.html
711 /// [reference]: https://doc.rust-lang.org/reference/items/structs.html
712 mod struct_keyword { }