diff --git a/src/rust-2018/data-types/inclusive-ranges.md b/src/rust-2018/data-types/inclusive-ranges.md index ee460caa..740877d5 100644 --- a/src/rust-2018/data-types/inclusive-ranges.md +++ b/src/rust-2018/data-types/inclusive-ranges.md @@ -5,7 +5,7 @@ Since well before Rust 1.0, you’ve been able to create exclusive ranges with `..` like this: -``` +```rust for i in 1..3 { println!("i: {}", i); } diff --git a/src/rust-2018/edition-changes.md b/src/rust-2018/edition-changes.md index 4e3288e2..7c4d1762 100644 --- a/src/rust-2018/edition-changes.md +++ b/src/rust-2018/edition-changes.md @@ -11,9 +11,11 @@ the 2018 edition compared to the 2015 edition. - [Anonymous trait function parameters] are not allowed. - [Trait function parameters] may use any irrefutable pattern when the function has a body. -- [`dyn`] is a [strict keyword], in 2015 it is a [weak keyword]. -- `async`, `await`, and `try` are [reserved keywords]. -- The following lints are now deny by default: +- Keyword changes: + - [`dyn`] is a [strict keyword][strict], in 2015 it is a [weak keyword]. + - `async` and `await` are [strict keywords][strict]. + - `try` is a [reserved keyword]. +- The following lints are now a hard error that you cannot silence: - [tyvar_behind_raw_pointer] ## Cargo @@ -28,7 +30,7 @@ the 2018 edition compared to the 2015 edition. [Path changes]: module-system/path-clarity.md [Trait function parameters]: https://doc.rust-lang.org/stable/reference/items/traits.html#parameter-patterns [`dyn`]: trait-system/dyn-trait-for-trait-objects.md -[reserved keywords]: https://doc.rust-lang.org/reference/keywords.html#reserved-keywords -[strict keyword]: https://doc.rust-lang.org/reference/keywords.html#strict-keywords +[reserved keyword]: https://doc.rust-lang.org/reference/keywords.html#reserved-keywords +[strict]: https://doc.rust-lang.org/reference/keywords.html#strict-keywords [tyvar_behind_raw_pointer]: https://github.com/rust-lang/rust/issues/46906 [weak keyword]: https://doc.rust-lang.org/reference/keywords.html#weak-keywords diff --git a/src/rust-2018/module-system/path-clarity.md b/src/rust-2018/module-system/path-clarity.md index 19bbf739..0a87e46d 100644 --- a/src/rust-2018/module-system/path-clarity.md +++ b/src/rust-2018/module-system/path-clarity.md @@ -65,26 +65,40 @@ keep doing what you were doing there as well. #### An exception There's one exception to this rule, and that's the "sysroot" crates. These are the -crates distributed with Rust itself. We'd eventually like to remove the requirement -for `extern crate` for them as well, but it hasn't shipped yet. - -You'll need to use `extern crate` for: - -* `proc_macro` - -Additionally, you would need to use it for: - -* `core` -* `std` - -However, `extern crate std;` is already implicit, and with `#![no_std]`, -`extern crate core;` is already implicit. You'll only need these in highly -specialized situations. - -Finally, on nightly, you'll need it for crates like: - -* `alloc` -* `test` +crates distributed with Rust itself. + +Usually these are only needed in very specialized situations. Starting in +1.41, `rustc` accepts the `--extern=CRATE_NAME` flag which automatically adds +the given crate name in a way similar to `extern crate`. Build tools may use +this to inject sysroot crates into the crate's prelude. Cargo does not have a +general way to express this, though it uses it for `proc_macro` crates. + +Some examples of needing to explicitly import sysroot crates are: + +* [`std`]: Usually this is not neccesary, because `std` is automatically + imported unless the crate is marked with [`#![no_std]`][no_std]. +* [`core`]: Usually this is not necessary, because `core` is automatically + imported, unless the crate is marked with [`#![no_core]`][no_core]. For + example, some of the internal crates used by the standard library itself + need this. +* [`proc_macro`]: This is automatically imported by Cargo if it is a + proc-macro crate starting in 1.42. `extern crate proc_macro;` would be + needed if you want to support older releases, or if using another build tool + that does not pass the appropriate `--extern` flags to `rustc`. +* [`alloc`]: Items in the `alloc` crate are usually accessed via re-exports in + the `std` crate. If you are working with a `no_std` crate that supports + allocation, then you may need to explicitly import `alloc`. +* [`test`]: This is only available on the [nightly channel], and is usually + only used for the unstable benchmark support. + +[`alloc`]: ../../../alloc/index.html +[`core`]: ../../../core/index.html +[`proc_macro`]: ../../../proc_macro/index.html +[`std`]: ../../../std/index.html +[`test`]: ../../../test/index.html +[nightly channel]: ../../../book/appendix-07-nightly-rust.html +[no_core]: https://github.com/rust-lang/rust/issues/29639 +[no_std]: ../../../reference/crates-and-source-files.html#preludes-and-no_std #### Macros diff --git a/src/rust-2018/rustdoc/documentation-tests-can-now-compile-fail.md b/src/rust-2018/rustdoc/documentation-tests-can-now-compile-fail.md index 35207c61..7fbf4fb7 100644 --- a/src/rust-2018/rustdoc/documentation-tests-can-now-compile-fail.md +++ b/src/rust-2018/rustdoc/documentation-tests-can-now-compile-fail.md @@ -4,7 +4,7 @@ You can now create `compile-fail` tests in Rustdoc, like this: -``` +```rust /// ```compile_fail /// let x = 5; /// x += 2; // shouldn't compile! diff --git a/src/rust-next/const-fn.md b/src/rust-next/const-fn.md index 65f640f7..13bb50bc 100644 --- a/src/rust-next/const-fn.md +++ b/src/rust-next/const-fn.md @@ -6,7 +6,7 @@ Expanded in many releases, see each aspect below for more details. A `const fn` allows you to execute code in a "const context." For example: -``` +```rust const fn five() -> i32 { 5 } @@ -34,7 +34,7 @@ that it is becoming more `const` over time. You can do arithmetic on integer literals: -``` +```rust const fn foo() -> i32 { 5 + 6 } @@ -46,7 +46,7 @@ const fn foo() -> i32 { You can use boolean operators other than `&&` and `||`, because they short-circut evaluation: -``` +```rust const fn mask(val: u8) -> u8 { let mask = 0x0f; @@ -60,7 +60,7 @@ const fn mask(val: u8) -> u8 { You can create arrays, structs, enums, and tuples: -``` +```rust struct Point { x: i32, y: i32, @@ -92,7 +92,7 @@ const fn foo() { You can call `const fn` from a `const fn`: -``` +```rust const fn foo() -> i32 { 5 } @@ -108,7 +108,7 @@ const fn bar() -> i32 { You can index into an array or slice: -``` +```rust const fn foo() -> i32 { let array = [1, 2, 3]; @@ -122,7 +122,7 @@ const fn foo() -> i32 { You can access parts of a struct or tuple: -``` +```rust struct Point { x: i32, y: i32, @@ -147,7 +147,7 @@ const fn foo() { You can read from a constant: -``` +```rust const FOO: i32 = 5; const fn foo() -> i32 { @@ -163,7 +163,7 @@ Note that this is *only* `const`, not `static`. You can create and de-reference references: -``` +```rust const fn foo(r: &i32) { *r; @@ -177,7 +177,7 @@ const fn foo(r: &i32) { You may cast things, except for raw pointers may not be casted to an integer: -``` +```rust const fn foo() { let x: usize = 5; @@ -191,7 +191,7 @@ const fn foo() { You can use irrefutable patterns that destructure values. For example: -``` +```rust const fn foo((x, y): (u8, u8)) { // ... } @@ -206,7 +206,7 @@ place that uses irrefutable patterns. You can use both mutable and immutable `let` bindings: -``` +```rust const fn foo() { let x = 5; let mut y = 10; @@ -219,7 +219,7 @@ const fn foo() { You can use assignment and assignment operators: -``` +```rust const fn foo() { let mut x = 5; x = 10; @@ -232,7 +232,7 @@ const fn foo() { You can call an `unsafe fn` inside a `const fn`: -``` +```rust const unsafe fn foo() -> i32 { 5 } const fn bar() -> i32 {