@@ -53,7 +53,7 @@ Here `x` represents some variable, `LV.f` is a field reference,
53
53
and ` *LV ` is a pointer dereference. There is no auto-deref or other
54
54
niceties. This means that if you have a type like:
55
55
56
- ``` text
56
+ ``` rust
57
57
struct S { f : uint }
58
58
```
59
59
@@ -82,13 +82,13 @@ SD = struct S<'LT...> { (f: TY)... }
82
82
83
83
Now, imagine we had a program like this:
84
84
85
- ``` text
85
+ ``` rust
86
86
struct Foo { f : uint , g : uint }
87
87
...
88
88
'a : {
89
- let mut x: Box<Foo> = ...;
90
- let y = &mut (*x).f;
91
- x = ...;
89
+ let mut x : Box <Foo > = ... ;
90
+ let y = & mut (* x ). f;
91
+ x = ... ;
92
92
}
93
93
```
94
94
@@ -507,7 +507,7 @@ specify that the lifetime of the loan must be less than the lifetime
507
507
of the ` &Ty ` pointer. In simple cases, this clause is redundant, since
508
508
the ` LIFETIME() ` function will already enforce the required rule:
509
509
510
- ```
510
+ ``` rust
511
511
fn foo (point : & 'a Point ) -> & 'static f32 {
512
512
& point . x // Error
513
513
}
@@ -517,7 +517,7 @@ The above example fails to compile both because of clause (1) above
517
517
but also by the basic ` LIFETIME() ` check. However, in more advanced
518
518
examples involving multiple nested pointers, clause (1) is needed:
519
519
520
- ```
520
+ ``` rust
521
521
fn foo (point : & 'a & 'b mut Point ) -> & 'b f32 {
522
522
& point . x // Error
523
523
}
@@ -536,7 +536,7 @@ which is only `'a`, not `'b`. Hence this example yields an error.
536
536
As a final twist, consider the case of two nested * immutable*
537
537
pointers, rather than a mutable pointer within an immutable one:
538
538
539
- ```
539
+ ``` rust
540
540
fn foo (point : & 'a & 'b Point ) -> & 'b f32 {
541
541
& point . x // OK
542
542
}
@@ -558,7 +558,7 @@ The rules pertaining to `LIFETIME` exist to ensure that we don't
558
558
create a borrowed pointer that outlives the memory it points at. So
559
559
` LIFETIME ` prevents a function like this:
560
560
561
- ```
561
+ ``` rust
562
562
fn get_1 <'a >() -> & 'a int {
563
563
let x = 1 ;
564
564
& x
@@ -578,7 +578,7 @@ after we return and hence the remaining code in `'a` cannot possibly
578
578
mutate it. This distinction is important for type checking functions
579
579
like this one:
580
580
581
- ```
581
+ ``` rust
582
582
fn inc_and_get <'a >(p : & 'a mut Point ) -> & 'a int {
583
583
p . x += 1 ;
584
584
& p . x
@@ -631,7 +631,7 @@ maximum of `LT'`.
631
631
632
632
Here is a concrete example of a bug this rule prevents:
633
633
634
- ```
634
+ ``` rust
635
635
// Test region-reborrow-from-shorter-mut-ref.rs:
636
636
fn copy_pointer <'a ,'b ,T >(x : & 'a mut & 'b mut T ) -> & 'b mut T {
637
637
& mut * * p // ERROR due to clause (1)
@@ -659,7 +659,7 @@ ways to violate the rules is to move the base pointer to a new name
659
659
and access it via that new name, thus bypassing the restrictions on
660
660
the old name. Here is an example:
661
661
662
- ```
662
+ ``` rust
663
663
// src/test/compile-fail/borrowck-move-mut-base-ptr.rs
664
664
fn foo (t0 : & mut int ) {
665
665
let p : & int = & * t0 ; // Freezes `*t0`
@@ -679,7 +679,7 @@ danger is to mutably borrow the base path. This can lead to two bad
679
679
scenarios. The most obvious is that the mutable borrow itself becomes
680
680
another path to access the same data, as shown here:
681
681
682
- ```
682
+ ``` rust
683
683
// src/test/compile-fail/borrowck-mut-borrow-of-mut-base-ptr.rs
684
684
fn foo <'a >(mut t0 : & 'a mut int ,
685
685
mut t1 : & 'a mut int ) {
@@ -700,7 +700,7 @@ of `t0`. Hence the claim `&mut t0` is illegal.
700
700
Another danger with an ` &mut ` pointer is that we could swap the ` t0 `
701
701
value away to create a new path:
702
702
703
- ```
703
+ ``` rust
704
704
// src/test/compile-fail/borrowck-swap-mut-base-ptr.rs
705
705
fn foo <'a >(mut t0 : & 'a mut int ,
706
706
mut t1 : & 'a mut int ) {
@@ -718,7 +718,7 @@ careful to ensure this example is still illegal.
718
718
referent is claimed, even freezing the base pointer can be dangerous,
719
719
as shown in the following example:
720
720
721
- ```
721
+ ``` rust
722
722
// src/test/compile-fail/borrowck-borrow-of-mut-base-ptr.rs
723
723
fn foo <'a >(mut t0 : & 'a mut int ,
724
724
mut t1 : & 'a mut int ) {
@@ -741,7 +741,7 @@ which is clearly unsound.
741
741
However, it is not always unsafe to freeze the base pointer. In
742
742
particular, if the referent is frozen, there is no harm in it:
743
743
744
- ```
744
+ ``` rust
745
745
// src/test/run-pass/borrowck-borrow-of-mut-base-ptr-safe.rs
746
746
fn foo <'a >(mut t0 : & 'a mut int ,
747
747
mut t1 : & 'a mut int ) {
@@ -757,7 +757,7 @@ thing `t2` can be used for is to further freeze `*t0`, which is
757
757
already frozen. In particular, we cannot assign to ` *t0 ` through the
758
758
new alias ` t2 ` , as demonstrated in this test case:
759
759
760
- ```
760
+ ``` rust
761
761
// src/test/run-pass/borrowck-borrow-mut-base-ptr-in-aliasable-loc.rs
762
762
fn foo (t0 : & & mut int ) {
763
763
let t1 = t0 ;
@@ -830,7 +830,7 @@ moves/uninitializations of the variable that is being used.
830
830
831
831
Let's look at a simple example:
832
832
833
- ```
833
+ ``` rust
834
834
fn foo (a : Box <int >) {
835
835
let b : Box <int >; // Gen bit 0.
836
836
0 commit comments