Skip to content

Commit 6ba8a6e

Browse files
committed
Add stuff for Rust 1.33
Fixes #202
1 parent 0a8ab50 commit 6ba8a6e

File tree

3 files changed

+266
-1
lines changed

3 files changed

+266
-1
lines changed

src/SUMMARY.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@
9494
- [No jemalloc by default](rust-next/no-jemalloc.md)
9595
- [Uniform Paths](rust-next/uniform-paths.md)
9696
- [`literal` macro matcher](rust-next/literal-macro-matcher.md)
97-
- [`?` operator in macros](rust-next/qustion-mark-operator-in-macros.md)
97+
- [`?` operator in macros](rust-next/qustion-mark-operator-in-macros.md)
98+
- [const fn](rust-next/const-fn.md)
99+
- [Pinning](rust-next/pin.md)

src/rust-next/const-fn.md

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
# `const fn`
2+
3+
Initially added: ![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
4+
5+
Expanded in many releases, see each aspect below for more details.
6+
7+
A `const fn` allows you to execute code in a "const context." For example:
8+
9+
```
10+
const fn five() -> i32 {
11+
5
12+
}
13+
14+
const FIVE: i32 = five();
15+
```
16+
17+
You cannot execute arbitrary code; the reasons why boil down to "you can
18+
destroy the type system." The details are a bit too much to put here, but the
19+
core idea is that `const fn` started off allowing the absolutely minimal
20+
subset of the language, and has slowly added more abilities over time.
21+
Therefore, while you can create a `const fn` in Rust 1.31, you cannot do much
22+
with it. This is why we didn't add `const fn` to the Rust 2018 section; it
23+
truly didn't become useful until after the release of the 2018 edition. This
24+
means that if you read this document top to bottom, the earlier versions may
25+
describe restrictions that are relaxed in later versions.
26+
27+
Additionally, this has allowed more and more of the standard library to be
28+
made `const`, we won't put all of those changes here, but you should know
29+
that it is becoming more `const` over time.
30+
31+
## Arithmetic and comparison operators on integers
32+
33+
![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
34+
35+
You can do arithmetic on integer literals:
36+
37+
```
38+
const fn foo() -> i32 {
39+
5 + 6
40+
}
41+
```
42+
43+
## Many boolean operators
44+
45+
![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
46+
47+
You can use boolean operators other than `&&` and `||`, because they short-circut evaluation:
48+
49+
```
50+
const fn mask(val: u8) -> u8 {
51+
let mask = 0x0f;
52+
53+
mask & val
54+
}
55+
```
56+
57+
## Constructing arrays, structs, enums, and tuples
58+
59+
![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
60+
61+
You can create arrays, structs, enums, and tuples:
62+
63+
```
64+
struct Point {
65+
x: i32,
66+
y: i32,
67+
}
68+
69+
enum Error {
70+
Incorrect,
71+
FileNotFound,
72+
}
73+
74+
const fn foo() {
75+
let array = [1, 2, 3];
76+
77+
let point = Point {
78+
x: 5,
79+
y: 10,
80+
};
81+
82+
let error = Error::FileNotFound;
83+
84+
let tuple = (1, 2, 3);
85+
}
86+
```
87+
88+
## Calls to other const fns
89+
90+
![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
91+
92+
You can call `const fn` from a `const fn`:
93+
94+
95+
```
96+
const fn foo() -> i32 {
97+
5
98+
}
99+
100+
const fn bar() -> i32 {
101+
foo()
102+
}
103+
```
104+
105+
## Index expressions on arrays and slices
106+
107+
![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
108+
109+
You can index into an array or slice:
110+
111+
```
112+
const fn foo() -> i32 {
113+
let array = [1, 2, 3];
114+
115+
array[1]
116+
}
117+
```
118+
119+
## Field accesses on structs and tuples
120+
121+
![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
122+
123+
You can access parts of a struct or tuple:
124+
125+
```
126+
struct Point {
127+
x: i32,
128+
y: i32,
129+
}
130+
131+
const fn foo() {
132+
let point = Point {
133+
x: 5,
134+
y: 10,
135+
};
136+
137+
let tuple = (1, 2, 3);
138+
139+
point.x;
140+
tuple.0;
141+
}
142+
```
143+
144+
## Reading from constants
145+
146+
![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
147+
148+
You can read from a constant:
149+
150+
```
151+
const FOO: i32 = 5;
152+
153+
const fn foo() -> i32 {
154+
FOO
155+
}
156+
```
157+
158+
Note that this is *only* `const`, not `static`.
159+
160+
## & and * of references
161+
162+
![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
163+
164+
You can create and de-reference references:
165+
166+
```
167+
const fn foo(r: &i32) {
168+
*r;
169+
170+
&5;
171+
}
172+
```
173+
174+
## Casts, except for raw pointer to integer casts
175+
176+
![Minimum Rust version: 1.31](https://img.shields.io/badge/Minimum%20Rust%20Version-1.31-brightgreen.svg)
177+
178+
You may cast things, except for raw pointers may not be casted to an integer:
179+
180+
```
181+
const fn foo() {
182+
let x: usize = 5;
183+
184+
x as i32;
185+
}
186+
```
187+
188+
## Irrefutable destructuring patterns
189+
190+
![Minimum Rust version: 1.33](https://img.shields.io/badge/Minimum%20Rust%20Version-1.33-brightgreen.svg)
191+
192+
You can use irrefutable patterns that destructure values. For example:
193+
194+
```
195+
const fn foo((x, y): (u8, u8)) {
196+
// ...
197+
# unimplemented!();
198+
}
199+
```
200+
201+
Here, `foo` destructures the tuple into `x` and `y`. `if let` is another
202+
place that uses irrefutable patterns.
203+
204+
## `let` bindings
205+
206+
![Minimum Rust version: 1.33](https://img.shields.io/badge/Minimum%20Rust%20Version-1.33-brightgreen.svg)
207+
208+
You can use both mutable and immutable `let` bindings:
209+
210+
```
211+
const fn foo() {
212+
let x = 5;
213+
let mut y = 10;
214+
}
215+
```
216+
217+
## Assignment
218+
219+
![Minimum Rust version: 1.33](https://img.shields.io/badge/Minimum%20Rust%20Version-1.33-brightgreen.svg)
220+
221+
You can use assignment and assignment operators:
222+
223+
```
224+
const fn foo() {
225+
let mut x = 5;
226+
x = 10;
227+
}
228+
```
229+
230+
## Calling `unsafe fn`
231+
232+
![Minimum Rust version: 1.33](https://img.shields.io/badge/Minimum%20Rust%20Version-1.33-brightgreen.svg)
233+
234+
YOu can call an `unsafe fn` inside a `const fn`:
235+
236+
```
237+
const unsafe fn foo() -> i32 { 5 }
238+
239+
const fn bar() -> i32 {
240+
unsafe { foo() }
241+
}
242+
```

src/rust-next/pin.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Pinning
2+
3+
![Minimum Rust version: 1.33](https://img.shields.io/badge/Minimum%20Rust%20Version-1.33-brightgreen.svg)
4+
5+
Rust 1.33 introduced a new concept, implemented as two types:
6+
7+
* [`Pin<P>`](https://doc.rust-lang.org/std/pin/struct.Pin.html), a wrapper
8+
around a kind of pointer which makes that pointer "pin" its value in place,
9+
preventing the value referenced by that pointer from being moved.
10+
* [`Unpin`](https://doc.rust-lang.org/std/marker/trait.Unpin.html), types that
11+
are safe to be moved, even if they're pinned.
12+
13+
Most users will not interact with pinning directly, and so we won't explain
14+
more here. For the details, see the [documentation for
15+
`std::pin`](https://doc.rust-lang.org/std/pin/index.html).
16+
17+
What *is* useful to know about pinning is that it's a pre-requisite for
18+
`async`/`await`. Folks who write async libraries may need to learn about
19+
pinning, but folks using them generally shouldn't need to interact with this
20+
feature at all.
21+

0 commit comments

Comments
 (0)