@@ -40,18 +40,19 @@ struct Pet {
40
40
}
41
41
```
42
42
43
- And convert a ` Pet ` to and from JSON.
43
+ And convert a ` Pet ` to and from JSON because ` serde_json ` defined ` Serialize ` and
44
+ ` Deserialize ` in a procedural macro.
44
45
45
46
Rust expands on this by adding the ability to define two other kinds of
46
47
advanced macros, "attribute-like procedrual macros" and "function-like
47
48
procedural macros."
48
49
49
- Attribute-like macros are similar to custom derive macros, but instead of
50
- generating code for ` #[derive] ` , they allow you to create new, custom
51
- attributes of your own. They're also more flexible; derive only works for
52
- structs and enums; attributes can go on other places as well , like functions.
53
- As an example of using an attribute-like macro, you might have something like
54
- this when using a web application framework:
50
+ Attribute-like macros are similar to custom derive macros, but instead of generating code
51
+ for only the ` #[derive] ` attribute , they allow you to create new, custom attributes of
52
+ your own. They're also more flexible: derive only works for structs and enums, but
53
+ attributes can go on other places, like functions. As an example of using an
54
+ attribute-like macro, you might have something like this when using a web application
55
+ framework:
55
56
56
57
```
57
58
#[route(GET, "/")]
@@ -66,7 +67,7 @@ procedural macro. Its signature would look like this:
66
67
pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
67
68
```
68
69
69
- Here, we have two input ` TokenStreams ` ; the first is for the contents of the
70
+ Here, we have two input ` TokenStreams ` : the first is for the contents of the
70
71
attribute itself, that is, the ` GET, "/" ` stuff. The second is the body of the
71
72
thing the attribute is attached to, in this case, ` fn index() {} ` and the rest
72
73
of the function's body.
@@ -86,12 +87,12 @@ syntactically correct. This macro would be defined like this:
86
87
pub fn sql(input: TokenStream) -> TokenStream {
87
88
```
88
89
89
- This is similar to the derive macro's signature: we get in the tokens that
90
- are inside of the parentheses, and return the code we wanted to generate.
90
+ This is similar to the derive macro's signature: we get the tokens that
91
+ are inside of the parentheses and return the code we want to generate.
91
92
92
93
### ` use ` and macros
93
94
94
- You can now [ use ` use ` to bring macros into scope] [ externmacro ] . For example,
95
+ You can now [ bring macros into scope with the ` use ` keyword ] [ externmacro ] . For example,
95
96
to use ` serde-json ` 's ` json ` macro, you used to write:
96
97
97
98
``` rust
@@ -125,7 +126,7 @@ let john = json!({
125
126
});
126
127
```
127
128
128
- This brings macros more in line with other items, and removes the need for
129
+ This brings macros more in line with other items and removes the need for
129
130
` macro_use ` annotations.
130
131
131
132
[ externmacro ] : https://github.com/rust-lang/rust/pull/50911/
@@ -138,7 +139,7 @@ its rules felt awkward in practice. These changes are the first steps we're
138
139
taking to make the module system feel more straightforward.
139
140
140
141
[ ` mod.rs ` files are now optional] [ optionalmod ] . Imagine we had a ` foo `
141
- submodule, with a ` bar ` submodule of its own. The directory layout would
142
+ submodule with a ` bar ` submodule of its own. The directory layout would
142
143
look like this:
143
144
144
145
``` text
@@ -173,15 +174,14 @@ existed, the project would look like this:
173
174
```
174
175
175
176
Many users found the need to move ` foo.rs ` into ` foo/mod.rs ` to be an
176
- unncessary, strage requirement. With the new layout, you create ` src/foo ` and
177
- put ` bar.rs ` in it and you're done.
177
+ unncessary, strage requirement. With the new layout, you create ` src/foo ` ,
178
+ put ` bar.rs ` in it, and you're done.
178
179
179
180
[ optionalmod ] : https://github.com/rust-lang/rust/pull/54072
180
181
181
- There's two changes to ` use ` as well. Well, three changes: we already
182
- mentioned that you can use ` use ` to bring macros into scope in the macros
183
- section. There's two more. The first is that you can [ ` use ` an extern crate
184
- without needing ` :: ` ] [ nocoloncolon ] , that is:
182
+ There's two changes to ` use ` as well, in addition to the aforementioned change for
183
+ macros. The first is that you can now always [ ` use ` an extern crate without
184
+ ` :: ` ] [ nocoloncolon ] , that is:
185
185
186
186
``` rust
187
187
// old
@@ -215,21 +215,21 @@ mod foo {
215
215
use serde_json;
216
216
217
217
fn baz() {
218
- // the other option is to use `::serde_json`, so we're using an absolute path rather than
219
- // a relative one
218
+ // the other option is to use `::serde_json`, so we're using an absolute path
219
+ // rather than a relative one
220
220
let json = ::serde_json::from_str("...");
221
221
}
222
222
}
223
223
```
224
224
225
- Moving a function to a submodule and having your imports break was not a
226
- great experience. Now, ` use ` will check to see if the first part of the path
227
- and see if it's one of your ` extern crate ` s, and if it is, use it, regardless
228
- of where you're at in the module hierarchy.
225
+ Moving a function to a submodule and having your imports break was not a great
226
+ experience. Now, ` use ` will check the first part of the path and see if it's an `extern
227
+ crate`, and if it is, use it regardless of where you're at in the module hierarchy.
229
228
230
229
[ nocoloncolon ] : https://github.com/rust-lang/rust/pull/54404/
231
230
232
- Finally, you can also [ use ` crate ` with ` use ` ] [ usecrate ] :
231
+ Finally, [ ` use ` also supports bringing items into scope with paths starting with
232
+ ` crate ` ] [ usecrate ] :
233
233
234
234
``` rust
235
235
mod foo {
@@ -240,14 +240,17 @@ mod foo {
240
240
241
241
// old
242
242
use :: foo :: bar;
243
+ // or
244
+ use foo :: bar;
243
245
244
246
// new
245
247
use crate :: foo :: bar;
246
248
```
247
249
248
- The ` crate ` keyword at the start of the path indicates that you would like
249
- the path to start at your crate root. ` use ` previously would always start at
250
- the crate root, but other paths would start at the local path, meaning:
250
+ The ` crate ` keyword at the start of the path indicates that you would like the path to
251
+ start at your crate root. Previously, paths specified after ` use ` would always start at
252
+ the crate root, but paths referring to items directly would start at the local path,
253
+ meaning the behavior of paths was inconsistent:
251
254
252
255
``` rust
253
256
mod foo {
@@ -260,25 +263,29 @@ mod baz {
260
263
pub fn qux () {
261
264
// old
262
265
:: foo :: bar ();
266
+ // does not work, which is different than with `use`:
267
+ // foo::bar();
263
268
264
269
// new
265
270
crate :: foo :: bar ();
266
271
}
267
272
}
268
273
```
269
274
270
- This will hopefully make absolute paths a bit more clear, and remove some of
271
- the ugliness of leading ` :: ` .
275
+ Once this style becomes widely used, this will hopefully make absolute paths a bit more
276
+ clear and remove some of the ugliness of leading ` :: ` .
272
277
273
- All of these changes combined lead to a more straightforward understanding of
274
- how paths resolve. When you see a path like ` a::b::c ` , you can ask:
278
+ All of these changes combined lead to a more straightforward understanding of how paths
279
+ resolve. Wherever you see a path like ` a::b::c ` someplace other than a ` use ` statement,
280
+ you can ask:
275
281
276
282
* Is ` a ` the name of a crate? Then we're looking for ` b::c ` inside of it.
277
283
* Is ` a ` the keyword ` crate ` ? Then we're looking for ` b::c ` from the root of our crate.
278
284
* Otherwise, we're looking for ` a::b::c ` from the current spot in the module hierarchy.
279
285
280
- Since these rules apply uniformly everywhere, you'll need to tweak your
281
- imports much less when moving code around.
286
+ The old behavior of ` use ` paths always starting from the crate root still applies. But
287
+ after making a one-time switch to the new style, these rules will apply uniformly to
288
+ paths everywhere, and you'll need to tweak your imports much less when moving code around.
282
289
283
290
[ usecrate ] : https://github.com/rust-lang/rust/pull/54404/
284
291
@@ -299,9 +306,10 @@ fn r#for() {
299
306
r#for ();
300
307
```
301
308
302
- This doesn't have many use cases today, but will once you are trying to use a
303
- Rust 2015 crate with a Rust 2018 project, and vice-versa; we'll explain more
304
- in that upcoming blog post.
309
+ This doesn't have many use cases today, but will once you are trying to use a Rust 2015
310
+ crate with a Rust 2018 project and vice-versa because the set of keywords will be
311
+ different in the two editions; we'll explain more in the upcoming blog post about
312
+ Rust 2018.
305
313
306
314
[ rawidents ] : https://github.com/rust-lang/rust/pull/53236/
307
315
@@ -327,16 +335,15 @@ release](https://github.com/rust-lang/rust/blob/master/RELEASES.md#stabilized-ap
327
335
328
336
Additionally, the standard library has long had functions like ` trim_left ` to eliminate
329
337
whitespace on one side of some text. However, when considering RTL languages, the meaning
330
- of "right" and "left" get confusing. As such, we're introducing some new names for these
338
+ of "right" and "left" gets confusing. As such, we're introducing new names for these
331
339
APIs:
332
340
333
341
* ` trim_left ` -> ` trim_start `
334
342
* ` trim_right ` -> ` trim_end `
335
343
* ` trim_left_matches ` -> ` trim_start_matches `
336
344
* ` trim_right_matches ` -> ` trim_end_matches `
337
345
338
- We plan to deprecate (but not removing, of course) the old names in Rust
339
- 1.33.
346
+ We plan to deprecate (but not remove, of course) the old names in Rust 1.33.
340
347
341
348
See the [ detailed release notes] [ notes ] for more.
342
349
0 commit comments