Skip to content

Commit c57ade2

Browse files
committed
Clarifications, grammar, punctuation
1 parent cfe470c commit c57ade2

File tree

1 file changed

+48
-41
lines changed

1 file changed

+48
-41
lines changed

_posts/2018-10-25-Rust-1.30.md

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,19 @@ struct Pet {
4040
}
4141
```
4242

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.
4445

4546
Rust expands on this by adding the ability to define two other kinds of
4647
advanced macros, "attribute-like procedrual macros" and "function-like
4748
procedural macros."
4849

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:
5556

5657
```
5758
#[route(GET, "/")]
@@ -66,7 +67,7 @@ procedural macro. Its signature would look like this:
6667
pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
6768
```
6869

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
7071
attribute itself, that is, the `GET, "/"` stuff. The second is the body of the
7172
thing the attribute is attached to, in this case, `fn index() {}` and the rest
7273
of the function's body.
@@ -86,12 +87,12 @@ syntactically correct. This macro would be defined like this:
8687
pub fn sql(input: TokenStream) -> TokenStream {
8788
```
8889

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.
9192

9293
### `use` and macros
9394

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,
9596
to use `serde-json`'s `json` macro, you used to write:
9697

9798
```rust
@@ -125,7 +126,7 @@ let john = json!({
125126
});
126127
```
127128

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
129130
`macro_use` annotations.
130131

131132
[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
138139
taking to make the module system feel more straightforward.
139140

140141
[`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
142143
look like this:
143144

144145
```text
@@ -173,15 +174,14 @@ existed, the project would look like this:
173174
```
174175

175176
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.
178179

179180
[optionalmod]: https://github.com/rust-lang/rust/pull/54072
180181

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:
185185

186186
```rust
187187
// old
@@ -215,21 +215,21 @@ mod foo {
215215
use serde_json;
216216
217217
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
220220
let json = ::serde_json::from_str("...");
221221
}
222222
}
223223
```
224224

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.
229228

230229
[nocoloncolon]: https://github.com/rust-lang/rust/pull/54404/
231230

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]:
233233

234234
```rust
235235
mod foo {
@@ -240,14 +240,17 @@ mod foo {
240240

241241
// old
242242
use ::foo::bar;
243+
// or
244+
use foo::bar;
243245

244246
// new
245247
use crate::foo::bar;
246248
```
247249

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:
251254

252255
```rust
253256
mod foo {
@@ -260,25 +263,29 @@ mod baz {
260263
pub fn qux() {
261264
// old
262265
::foo::bar();
266+
// does not work, which is different than with `use`:
267+
// foo::bar();
263268

264269
// new
265270
crate::foo::bar();
266271
}
267272
}
268273
```
269274

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 `::`.
272277

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:
275281

276282
* Is `a` the name of a crate? Then we're looking for `b::c` inside of it.
277283
* Is `a` the keyword `crate`? Then we're looking for `b::c` from the root of our crate.
278284
* Otherwise, we're looking for `a::b::c` from the current spot in the module hierarchy.
279285

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.
282289

283290
[usecrate]: https://github.com/rust-lang/rust/pull/54404/
284291

@@ -299,9 +306,10 @@ fn r#for() {
299306
r#for();
300307
```
301308

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.
305313

306314
[rawidents]: https://github.com/rust-lang/rust/pull/53236/
307315

@@ -327,16 +335,15 @@ release](https://github.com/rust-lang/rust/blob/master/RELEASES.md#stabilized-ap
327335

328336
Additionally, the standard library has long had functions like `trim_left` to eliminate
329337
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
331339
APIs:
332340

333341
* `trim_left` -> `trim_start`
334342
* `trim_right` -> `trim_end`
335343
* `trim_left_matches` -> `trim_start_matches`
336344
* `trim_right_matches` -> `trim_end_matches`
337345

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.
340347

341348
See the [detailed release notes][notes] for more.
342349

0 commit comments

Comments
 (0)