Skip to content

Commit 5d56de9

Browse files
committed
Minor improvements
* Prefer compile_fail over ignore, to catch if the code accidentally starts compiling in future versions. * Make an example compile by providing mocked environment around it in case it should be correct. * Links into std.
1 parent c02e0e7 commit 5d56de9

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

src/borrow-splitting.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ println!("{} {} {} {}", a, b, c, c2);
2626
However borrowck doesn't understand arrays or slices in any way, so this doesn't
2727
work:
2828

29-
```rust,ignore
29+
```rust,compile_fail
3030
let mut x = [1, 2, 3];
3131
let a = &mut x[0];
3232
let b = &mut x[1];
@@ -60,7 +60,12 @@ the left of the index, and one for everything to the right. Intuitively we know
6060
this is safe because the slices don't overlap, and therefore alias. However
6161
the implementation requires some unsafety:
6262

63-
```rust,ignore
63+
```rust
64+
# use std::slice::from_raw_parts_mut;
65+
# struct FakeSlice<T>(T);
66+
# impl<T> FakeSlice<T> {
67+
# fn len(&self) -> usize { unimplemented!() }
68+
# fn as_mut_ptr(&mut self) -> *mut T { unimplemented!() }
6469
fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
6570
let len = self.len();
6671
let ptr = self.as_mut_ptr();
@@ -70,6 +75,7 @@ fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
7075
from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
7176
}
7277
}
78+
# }
7379
```
7480

7581
This is actually a bit subtle. So as to avoid ever making two `&mut`'s to the

src/checked-uninit.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Like C, all stack variables in Rust are uninitialized until a value is
44
explicitly assigned to them. Unlike C, Rust statically prevents you from ever
55
reading them until you do:
66

7-
```rust,ignore
7+
```rust,compile_fail
88
fn main() {
99
let x: i32;
1010
println!("{}", x);
@@ -39,7 +39,7 @@ fn main() {
3939

4040
but this doesn't:
4141

42-
```rust,ignore
42+
```rust,compile_fail
4343
fn main() {
4444
let x: i32;
4545
if true {

src/phantom-data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ types or lifetimes are logically associated with a struct, but not actually
55
part of a field. This most commonly occurs with lifetimes. For instance, the
66
`Iter` for `&'a [T]` is (approximately) defined as follows:
77

8-
```rust,ignore
8+
```rust,compile_fail
99
struct Iter<'a, T: 'a> {
1010
ptr: *const T,
1111
end: *const T,

src/transmutes.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ really can't emphasize that you should deeply think about finding Another Way
66
than the operations covered in this section. This is really, truly, the most
77
horribly unsafe thing you can do in Rust. The railguards here are dental floss.
88

9-
`mem::transmute<T, U>` takes a value of type `T` and reinterprets it to have
10-
type `U`. The only restriction is that the `T` and `U` are verified to have the
11-
same size. The ways to cause Undefined Behavior with this are mind boggling.
9+
[`mem::transmute<T, U>`][transmute] takes a value of type `T` and reinterprets
10+
it to have type `U`. The only restriction is that the `T` and `U` are verified
11+
to have the same size. The ways to cause Undefined Behavior with this are mind
12+
boggling.
1213

1314
* First and foremost, creating an instance of *any* type with an invalid state
1415
is going to cause arbitrary chaos that can't really be predicted.
@@ -23,13 +24,16 @@ same size. The ways to cause Undefined Behavior with this are mind boggling.
2324
* Transmuting to a reference without an explicitly provided lifetime
2425
produces an [unbounded lifetime]
2526

26-
`mem::transmute_copy<T, U>` somehow manages to be *even more* wildly unsafe than
27-
this. It copies `size_of<U>` bytes out of an `&T` and interprets them as a `U`.
28-
The size check that `mem::transmute` has is gone (as it may be valid to copy
29-
out a prefix), though it is Undefined Behavior for `U` to be larger than `T`.
27+
[`mem::transmute_copy<T, U>`][transmute_copy] somehow manages to be *even more*
28+
wildly unsafe than this. It copies `size_of<U>` bytes out of an `&T` and
29+
interprets them as a `U`. The size check that `mem::transmute` has is gone (as
30+
it may be valid to copy out a prefix), though it is Undefined Behavior for `U`
31+
to be larger than `T`.
3032

3133
Also of course you can get most of the functionality of these functions using
3234
pointer casts.
3335

3436

3537
[unbounded lifetime]: unbounded-lifetimes.html
38+
[transmute]: ../std/mem/fn.transmute.html
39+
[transmute_copy]: ../std/mem/fn.transmute.html

0 commit comments

Comments
 (0)