forked from rust-lang/rust
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Backport TRPL, reference, and grammar.
Rather than port each individual change to these files, for the release, I just waited to do it all at the end, in this commit. Since individual comits made it to master, everyone should get proper credit in the main tree, and AUTHORS includes those whose changes are here.
- Loading branch information
1 parent
6e2106d
commit 665aed059a09db34ba30e1dfd28bd11954bf8db3
Showing
62 changed files
with
6,688 additions
and
2,110 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| % Associated Constants | ||
|
|
||
| With the `associated_consts` feature, you can define constants like this: | ||
|
|
||
| ```rust | ||
| #![feature(associated_consts)] | ||
| trait Foo { | ||
| const ID: i32; | ||
| } | ||
| impl Foo for i32 { | ||
| const ID: i32 = 1; | ||
| } | ||
| fn main() { | ||
| assert_eq!(1, i32::ID); | ||
| } | ||
| ``` | ||
|
|
||
| Any implementor of `Foo` will have to define `ID`. Without the definition: | ||
|
|
||
| ```rust,ignore | ||
| #![feature(associated_consts)] | ||
| trait Foo { | ||
| const ID: i32; | ||
| } | ||
| impl Foo for i32 { | ||
| } | ||
| ``` | ||
|
|
||
| gives | ||
|
|
||
| ```text | ||
| error: not all trait items implemented, missing: `ID` [E0046] | ||
| impl Foo for i32 { | ||
| } | ||
| ``` | ||
|
|
||
| A default value can be implemented as well: | ||
|
|
||
| ```rust | ||
| #![feature(associated_consts)] | ||
| trait Foo { | ||
| const ID: i32 = 1; | ||
| } | ||
| impl Foo for i32 { | ||
| } | ||
| impl Foo for i64 { | ||
| const ID: i32 = 5; | ||
| } | ||
| fn main() { | ||
| assert_eq!(1, i32::ID); | ||
| assert_eq!(5, i64::ID); | ||
| } | ||
| ``` | ||
|
|
||
| As you can see, when implementing `Foo`, you can leave it unimplemented, as | ||
| with `i32`. It will then use the default value. But, as in `i64`, we can also | ||
| add our own definition. | ||
|
|
||
| Associated constants don’t have to be associated with a trait. An `impl` block | ||
| for a `struct` works fine too: | ||
|
|
||
| ```rust | ||
| #![feature(associated_consts)] | ||
| struct Foo; | ||
| impl Foo { | ||
| pub const FOO: u32 = 3; | ||
| } | ||
| ``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,70 @@ | ||
| % Attributes | ||
|
|
||
| Coming Soon! | ||
| Declarations can be annotated with ‘attributes’ in Rust. They look like this: | ||
|
|
||
| ```rust | ||
| #[test] | ||
| # fn foo() {} | ||
| ``` | ||
|
|
||
| or like this: | ||
|
|
||
| ```rust | ||
| # mod foo { | ||
| #![test] | ||
| # } | ||
| ``` | ||
|
|
||
| The difference between the two is the `!`, which changes what the attribute | ||
| applies to: | ||
|
|
||
| ```rust,ignore | ||
| #[foo] | ||
| struct Foo; | ||
| mod bar { | ||
| #![bar] | ||
| } | ||
| ``` | ||
|
|
||
| The `#[foo]` attribute applies to the next item, which is the `struct` | ||
| declaration. The `#![bar]` attribute applies to the item enclosing it, which is | ||
| the `mod` declaration. Otherwise, they’re the same. Both change the meaning of | ||
| the item they’re attached to somehow. | ||
|
|
||
| For example, consider a function like this: | ||
|
|
||
| ```rust | ||
| #[test] | ||
| fn check() { | ||
| assert_eq!(2, 1 + 1); | ||
| } | ||
| ``` | ||
|
|
||
| It is marked with `#[test]`. This means it’s special: when you run | ||
| [tests][tests], this function will execute. When you compile as usual, it won’t | ||
| even be included. This function is now a test function. | ||
|
|
||
| [tests]: testing.html | ||
|
|
||
| Attributes may also have additional data: | ||
|
|
||
| ```rust | ||
| #[inline(always)] | ||
| fn super_fast_fn() { | ||
| # } | ||
| ``` | ||
|
|
||
| Or even keys and values: | ||
|
|
||
| ```rust | ||
| #[cfg(target_os = "macos")] | ||
| mod macos_only { | ||
| # } | ||
| ``` | ||
|
|
||
| Rust attributes are used for a number of different things. There is a full list | ||
| of attributes [in the reference][reference]. Currently, you are not allowed to | ||
| create your own attributes, the Rust compiler defines them. | ||
|
|
||
| [reference]: ../reference.html#attributes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -13,7 +13,7 @@ pub fn add_two(a: i32) -> i32 { | ||
| } | ||
| #[cfg(test)] | ||
| mod test { | ||
| mod tests { | ||
| use super::*; | ||
| use test::Bencher; | ||
Oops, something went wrong.