426
426
x::y::z;
427
427
```
428
428
429
- Path components are usually [ identifiers] ( #identifiers ) , but the trailing
430
- component of a path may be an angle-bracket-enclosed list of type arguments. In
431
- [ expression] ( #expressions ) context, the type argument list is given after a
432
- final ( ` :: ` ) namespace qualifier in order to disambiguate it from a relational
433
- expression involving the less-than symbol (` < ` ). In type expression context,
434
- the final namespace qualifier is omitted.
429
+ Path components are usually [ identifiers] ( #identifiers ) , but they may
430
+ also include angle-bracket-enclosed lists of type arguments. In
431
+ [ expression] ( #expressions ) context, the type argument list is given
432
+ after a ` :: ` namespace qualifier in order to disambiguate it from a
433
+ relational expression involving the less-than symbol (` < ` ). In type
434
+ expression context, the final namespace qualifier is omitted.
435
435
436
436
Two examples of paths with type arguments:
437
437
@@ -497,8 +497,9 @@ names, and invoked through a consistent syntax: `some_extension!(...)`.
497
497
498
498
Users of ` rustc ` can define new syntax extensions in two ways:
499
499
500
- * [ Compiler plugins] [ plugin ] can include arbitrary
501
- Rust code that manipulates syntax trees at compile time.
500
+ * [ Compiler plugins] [ plugin ] can include arbitrary Rust code that
501
+ manipulates syntax trees at compile time. Note that the interface
502
+ for compiler plugins is considered highly unstable.
502
503
503
504
* [ Macros] ( book/macros.html ) define new syntax in a higher-level,
504
505
declarative way.
@@ -560,14 +561,18 @@ Nested repetitions are allowed.
560
561
The parser used by the macro system is reasonably powerful, but the parsing of
561
562
Rust syntax is restricted in two ways:
562
563
563
- 1 . The parser will always parse as much as possible. If it attempts to match
564
- ` $i:expr [ , ] ` against ` 8 [ , ] ` , it will attempt to parse ` i ` as an array
565
- index operation and fail. Adding a separator can solve this problem.
564
+ 1 . Macro definitions are required to include suitable separators after parsing
565
+ expressions and other bits of the Rust grammar. This implies that
566
+ a macro definition like ` $i:expr [ , ] ` is not legal, because ` [ ` could be part
567
+ of an expression. A macro definition like ` $i:expr, ` or ` $i:expr; ` would be legal,
568
+ however, because ` , ` and ` ; ` are legal separators. See [ RFC 550] for more information.
566
569
2 . The parser must have eliminated all ambiguity by the time it reaches a ` $ `
567
570
_ name_ ` : ` _ designator_ . This requirement most often affects name-designator
568
571
pairs when they occur at the beginning of, or immediately after, a ` $(...)* ` ;
569
572
requiring a distinctive token in front can solve the problem.
570
573
574
+ [ RFC 550 ] : https://github.com/rust-lang/rfcs/blob/master/text/0550-macro-future-proofing.md
575
+
571
576
# Crates and source files
572
577
573
578
Although Rust, like any other language, can be implemented by an interpreter as
@@ -686,7 +691,8 @@ type arguments as a list of comma-separated types enclosed within angle
686
691
brackets, in order to refer to the type-parameterized item. In practice, the
687
692
type-inference system can usually infer such argument types from context. There
688
693
are no general type-parametric types, only type-parametric items. That is, Rust
689
- has no notion of type abstraction: there are no first-class "forall" types.
694
+ has no notion of type abstraction: there are no higher-ranked (or "forall") types
695
+ abstracted over other types, though higher-ranked types do exist for lifetimes.
690
696
691
697
### Modules
692
698
@@ -732,6 +738,7 @@ mod vec;
732
738
733
739
mod thread {
734
740
// Load the `local_data` module from `thread/local_data.rs`
741
+ // or `thread/local_data/mod.rs`.
735
742
mod local_data;
736
743
}
737
744
```
@@ -1004,7 +1011,8 @@ the guarantee that these issues are never caused by safe code.
1004
1011
* ` &mut ` and ` & ` follow LLVM’s scoped [ noalias] model, except if the ` &T `
1005
1012
contains an ` UnsafeCell<U> ` . Unsafe code must not violate these aliasing
1006
1013
guarantees.
1007
- * Mutating an immutable value/reference without ` UnsafeCell<U> `
1014
+ * Mutating non-mutable data (that is, data reached through a shared reference or
1015
+ data owned by a ` let ` binding), unless that data is contained within an ` UnsafeCell<U> ` .
1008
1016
* Invoking undefined behavior via compiler intrinsics:
1009
1017
* Indexing outside of the bounds of an object with ` std::ptr::offset `
1010
1018
(` offset ` intrinsic), with
@@ -1034,9 +1042,13 @@ be undesired.
1034
1042
* Exiting without calling destructors
1035
1043
* Sending signals
1036
1044
* Accessing/modifying the file system
1037
- * Unsigned integer overflow (well-defined as wrapping)
1038
- * Signed integer overflow (well-defined as two’s complement representation
1039
- wrapping)
1045
+ * Integer overflow
1046
+ - Overflow is considered "unexpected" behavior and is always user-error,
1047
+ unless the ` wrapping ` primitives are used. In non-optimized builds, the compiler
1048
+ will insert debug checks that panic on overflow, but in optimized builds overflow
1049
+ instead results in wrapped values. See [ RFC 560] for the rationale and more details.
1050
+
1051
+ [ RFC 560 ] : https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md
1040
1052
1041
1053
#### Diverging functions
1042
1054
@@ -1310,11 +1322,26 @@ type of the value is not required to ascribe to `Sync`.
1310
1322
1311
1323
### Traits
1312
1324
1313
- A _ trait_ describes a set of method types.
1325
+ A _ trait_ describes an abstract interface that types can
1326
+ implement. This interface consists of associated items, which come in
1327
+ three varieties:
1328
+
1329
+ - functions
1330
+ - constants
1331
+ - types
1332
+
1333
+ Associated functions whose first parameter is named ` self ` are called
1334
+ methods and may be invoked using ` . ` notation (e.g., ` x.foo() ` ).
1335
+
1336
+ All traits define an implicit type parameter ` Self ` that refers to
1337
+ "the type that is implementing this interface". Traits may also
1338
+ contain additional type parameters. These type parameters (including
1339
+ ` Self ` ) may be constrained by other traits and so forth as usual.
1314
1340
1315
- Traits can include default implementations of methods, written in terms of some
1316
- unknown [ ` self ` type] ( #self-types ) ; the ` self ` type may either be completely
1317
- unspecified, or constrained by some other trait.
1341
+ Trait bounds on ` Self ` are considered "supertraits". These are
1342
+ required to be acyclic. Supertraits are somewhat different from other
1343
+ constraints in that they affect what methods are available in the
1344
+ vtable when the trait is used as a [ trait object] ( #trait-objects ) .
1318
1345
1319
1346
Traits are implemented for specific types through separate
1320
1347
[ implementations] ( #implementations ) .
@@ -1359,15 +1386,18 @@ fn draw_twice<T: Shape>(surface: Surface, sh: T) {
1359
1386
}
1360
1387
```
1361
1388
1362
- Traits also define an [ trait object] ( #trait-objects ) with the same name as the
1363
- trait. Values of this type are created by [ casting] ( #type-cast-expressions )
1364
- pointer values (pointing to a type for which an implementation of the given
1365
- trait is in scope) to pointers to the trait name, used as a type.
1389
+ Traits also define an [ trait object] ( #trait-objects ) with the same
1390
+ name as the trait. Values of this type are created by coercing from a
1391
+ pointer of some specific type to a pointer of trait type. For example,
1392
+ ` &T ` could be coerced to ` &Shape ` if ` T: Shape ` holds (and similarly
1393
+ for ` Box<T> ` ). This coercion can either be implicit or
1394
+ [ explicit] ( #type-cast-expressions ) . Here is an example of an explicit
1395
+ coercion:
1366
1396
1367
1397
```
1368
- # trait Shape { fn dummy(&self) { } }
1369
- # impl Shape for i32 { }
1370
- # let mycircle = 0i32;
1398
+ trait Shape { }
1399
+ impl Shape for i32 { }
1400
+ let mycircle = 0i32;
1371
1401
let myshape: Box<Shape> = Box::new(mycircle) as Box<Shape>;
1372
1402
```
1373
1403
@@ -2041,7 +2071,8 @@ The name `str_eq` has a special meaning to the Rust compiler, and the presence
2041
2071
of this definition means that it will use this definition when generating calls
2042
2072
to the string equality function.
2043
2073
2044
- A complete list of the built-in language items will be added in the future.
2074
+ The set of language items is currently considered unstable. A complete
2075
+ list of the built-in language items will be added in the future.
2045
2076
2046
2077
### Inline attributes
2047
2078
@@ -2053,11 +2084,6 @@ The compiler automatically inlines functions based on internal heuristics.
2053
2084
Incorrectly inlining functions can actually make the program slower, so it
2054
2085
should be used with care.
2055
2086
2056
- Immutable statics are always considered inlineable unless marked with
2057
- ` #[inline(never)] ` . It is undefined whether two different inlineable statics
2058
- have the same memory address. In other words, the compiler is free to collapse
2059
- duplicate inlineable statics together.
2060
-
2061
2087
` #[inline] ` and ` #[inline(always)] ` always cause the function to be serialized
2062
2088
into the crate metadata to allow cross-crate inlining.
2063
2089
@@ -2259,10 +2285,6 @@ The currently implemented features of the reference compiler are:
2259
2285
* ` unboxed_closures ` - Rust's new closure design, which is currently a work in
2260
2286
progress feature with many known bugs.
2261
2287
2262
- * ` unsafe_destructor ` - Allows use of the ` #[unsafe_destructor] ` attribute,
2263
- which is considered wildly unsafe and will be
2264
- obsoleted by language improvements.
2265
-
2266
2288
* ` unsafe_no_drop_flag ` - Allows use of the ` #[unsafe_no_drop_flag] ` attribute,
2267
2289
which removes hidden flag added to a type that
2268
2290
implements the ` Drop ` trait. The design for the
@@ -2382,18 +2404,54 @@ expressions](#index-expressions) (`expr[expr]`), and [field
2382
2404
references] ( #field-expressions ) (` expr.f ` ). All other expressions are rvalues.
2383
2405
2384
2406
The left operand of an [ assignment] ( #assignment-expressions ) or
2385
- [ compound-assignment] ( #compound-assignment-expressions ) expression is an lvalue
2386
- context, as is the single operand of a unary
2387
- [ borrow] ( #unary-operator-expressions ) . All other expression contexts are
2388
- rvalue contexts.
2407
+ [ compound-assignment] ( #compound-assignment-expressions ) expression is
2408
+ an lvalue context, as is the single operand of a unary
2409
+ [ borrow] ( #unary-operator-expressions ) . The discriminant or subject of
2410
+ a [ match expression] ( #match-expressions ) may be an lvalue context, if
2411
+ ref bindings are made, but is otherwise an rvalue context. All other
2412
+ expression contexts are rvalue contexts.
2389
2413
2390
2414
When an lvalue is evaluated in an _ lvalue context_ , it denotes a memory
2391
2415
location; when evaluated in an _ rvalue context_ , it denotes the value held _ in_
2392
2416
that memory location.
2393
2417
2394
- When an rvalue is used in an lvalue context, a temporary un-named lvalue is
2395
- created and used instead. A temporary's lifetime equals the largest lifetime
2396
- of any reference that points to it.
2418
+ ##### Temporary lifetimes
2419
+
2420
+ When an rvalue is used in an lvalue context, a temporary un-named
2421
+ lvalue is created and used instead. The lifetime of temporary values
2422
+ is typically the innermost enclosing statement; the tail expression of
2423
+ a block is considered part of the statement that encloses the block.
2424
+
2425
+ When a temporary rvalue is being created that is assigned into a ` let `
2426
+ declaration, however, the temporary is created with the lifetime of
2427
+ the enclosing block instead, as using the enclosing statement (the
2428
+ ` let ` declaration) would be a guaranteed error (since a pointer to the
2429
+ temporary would be stored into a variable, but the temporary would be
2430
+ freed before the variable could be used). The compiler uses simple
2431
+ syntactic rules to decide which values are being assigned into a ` let `
2432
+ binding, and therefore deserve a longer temporary lifetime.
2433
+
2434
+ Here are some examples:
2435
+
2436
+ - ` let x = foo(&temp()) ` . The expression ` temp() ` is an rvalue. As it
2437
+ is being borrowed, a temporary is created which will be freed after
2438
+ the innermost enclosing statement (the ` let ` declaration, in this case).
2439
+ - ` let x = temp().foo() ` . This is the same as the previous example,
2440
+ except that the value of ` temp() ` is being borrowed via autoref on a
2441
+ method-call. Here we are assuming that ` foo() ` is an ` &self ` method
2442
+ defined in some trait, say ` Foo ` . In other words, the expression
2443
+ ` temp().foo() ` is equivalent to ` Foo::foo(&temp()) ` .
2444
+ - ` let x = &temp() ` . Here, the same temporary is being assigned into
2445
+ ` x ` , rather than being passed as a parameter, and hence the
2446
+ temporary's lifetime is considered to be the enclosing block.
2447
+ - ` let x = SomeStruct { foo: &temp() } ` . As in the previous case, the
2448
+ temporary is assigned into a struct which is then assigned into a
2449
+ binding, and hence it is given the lifetime of the enclosing block.
2450
+ - ` let x = [ &temp() ] ` . As in the previous case, the
2451
+ temporary is assigned into an array which is then assigned into a
2452
+ binding, and hence it is given the lifetime of the enclosing block.
2453
+ - ` let ref x = temp() ` . In this case, the temporary is created using a ref binding,
2454
+ but the result is the same: the lifetime is extended to the enclosing block.
2397
2455
2398
2456
#### Moved and copied types
2399
2457
@@ -2535,8 +2593,10 @@ A field access is an [lvalue](#lvalues,-rvalues-and-temporaries) referring to
2535
2593
the value of that field. When the type providing the field inherits mutability,
2536
2594
it can be [ assigned] ( #assignment-expressions ) to.
2537
2595
2538
- Also, if the type of the expression to the left of the dot is a pointer, it is
2539
- automatically dereferenced to make the field access possible.
2596
+ Also, if the type of the expression to the left of the dot is a
2597
+ pointer, it is automatically dereferenced as many times as necessary
2598
+ to make the field access possible. In cases of ambiguity, we prefer
2599
+ fewer autoderefs to more.
2540
2600
2541
2601
### Array expressions
2542
2602
@@ -2577,6 +2637,11 @@ let arr = ["a", "b"];
2577
2637
arr[10]; // panics
2578
2638
```
2579
2639
2640
+ Also, if the type of the expression to the left of the brackets is a
2641
+ pointer, it is automatically dereferenced as many times as necessary
2642
+ to make the indexing possible. In cases of ambiguity, we prefer fewer
2643
+ autoderefs to more.
2644
+
2580
2645
### Range expressions
2581
2646
2582
2647
The ` .. ` operator will construct an object of one of the ` std::ops::Range ` variants.
@@ -2599,7 +2664,7 @@ assert_eq!(x,y);
2599
2664
2600
2665
### Unary operator expressions
2601
2666
2602
- Rust defines three unary operators. They are all written as prefix operators,
2667
+ Rust defines the following unary operators. They are all written as prefix operators,
2603
2668
before the expression they apply to.
2604
2669
2605
2670
* ` - `
@@ -2613,11 +2678,20 @@ before the expression they apply to.
2613
2678
implemented by the type and required for an outer expression that will or
2614
2679
could mutate the dereference), and produces the result of dereferencing the
2615
2680
` & ` or ` &mut ` borrowed pointer returned from the overload method.
2616
-
2617
2681
* ` ! `
2618
2682
: Logical negation. On the boolean type, this flips between ` true ` and
2619
2683
` false ` . On integer types, this inverts the individual bits in the
2620
2684
two's complement representation of the value.
2685
+ * ` & ` and ` &mut `
2686
+ : Borrowing. When applied to an lvalue, these operators produce a
2687
+ reference (pointer) to the lvalue. The lvalue is also placed into
2688
+ a borrowed state for the duration of the reference. For a shared
2689
+ borrow (` & ` ), this implies that the lvalue may not be mutated, but
2690
+ it may be read or shared again. For a mutable borrow (` &mut ` ), the
2691
+ lvalue may not be accessed in any way until the borrow expires.
2692
+ If the ` & ` or ` &mut ` operators are applied to an rvalue, a
2693
+ temporary value is created; the lifetime of this temporary value
2694
+ is defined by [ syntactic rules] ( #temporary-lifetimes ) .
2621
2695
2622
2696
### Binary operator expressions
2623
2697
@@ -2727,6 +2801,13 @@ fn avg(v: &[f64]) -> f64 {
2727
2801
}
2728
2802
```
2729
2803
2804
+ Some of the conversions which can be done through the ` as ` operator
2805
+ can also be done implicitly at various points in the program, such as
2806
+ argument passing and assignment to a ` let ` binding with an explicit
2807
+ type. Implicit conversions are limited to "harmless" conversions that
2808
+ do not lose information and which have minimal or no risk of
2809
+ surprising side-effects on the dynamic execution semantics.
2810
+
2730
2811
#### Assignment expressions
2731
2812
2732
2813
An _ assignment expression_ consists of an
@@ -3348,6 +3429,22 @@ let bo: Binop = add;
3348
3429
x = bo(5,7);
3349
3430
```
3350
3431
3432
+ #### Function types for specific items
3433
+
3434
+ Internally to the compiler, there are also function types that are specific to a particular
3435
+ function item. In the following snippet, for example, the internal types of the functions
3436
+ ` foo ` and ` bar ` are different, despite the fact that they have the same signature:
3437
+
3438
+ ```
3439
+ fn foo() { }
3440
+ fn bar() { }
3441
+ ```
3442
+
3443
+ The types of ` foo ` and ` bar ` can both be implicitly coerced to the fn
3444
+ pointer type ` fn() ` . There is currently no syntax for unique fn types,
3445
+ though the compiler will emit a type like ` fn() {foo} ` in error
3446
+ messages to indicate "the unique fn type for the function ` foo ` ".
3447
+
3351
3448
### Closure types
3352
3449
3353
3450
A [ lambda expression] ( #lambda-expressions ) produces a closure value with
@@ -3432,8 +3529,9 @@ has type `Vec<A>`, a vector with element type `A`.
3432
3529
3433
3530
### Self types
3434
3531
3435
- The special type ` self ` has a meaning within methods inside an impl item. It
3436
- refers to the type of the implicit ` self ` argument. For example, in:
3532
+ The special type ` Self ` has a meaning within traits and impls. In a trait definition, it refers
3533
+ to an implicit type parameter representing the "implementing" type. In an impl,
3534
+ it is an alias for the implementing type. For example, in:
3437
3535
3438
3536
```
3439
3537
trait Printable {
@@ -3447,8 +3545,9 @@ impl Printable for String {
3447
3545
}
3448
3546
```
3449
3547
3450
- ` self ` refers to the value of type ` String ` that is the receiver for a call to
3451
- the method ` make_string ` .
3548
+ The notation ` &self ` is a shorthand for ` self: &Self ` . In this case,
3549
+ in the impl, ` Self ` refers to the value of type ` String ` that is the
3550
+ receiver for a call to the method ` make_string ` .
3452
3551
3453
3552
# Special traits
3454
3553
0 commit comments