mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-26 17:23:01 +02:00
Add documentation links to the important traits. (#377)
* Add documentation links to the important traits. * Also add links in the sub-pages.
This commit is contained in:
parent
fbb12161eb
commit
f912825411
@ -1,6 +1,6 @@
|
||||
# The `Default` Trait
|
||||
|
||||
`Default` trait provides a default implementation of a trait.
|
||||
[`Default`][1] trait provides a default implementation of a trait.
|
||||
|
||||
```rust,editable
|
||||
#[derive(Debug, Default)]
|
||||
@ -45,3 +45,5 @@ fn main() {
|
||||
* Rust standard library is aware that types can implement `Default` and provides convenience methods that use it.
|
||||
|
||||
</details>
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/default/trait.Default.html
|
||||
|
@ -1,6 +1,6 @@
|
||||
# The `Drop` Trait
|
||||
|
||||
Values which implement `Drop` can specify code to run when they go out of scope:
|
||||
Values which implement [`Drop`][1] can specify code to run when they go out of scope:
|
||||
|
||||
```rust,editable
|
||||
struct Droppable {
|
||||
@ -40,3 +40,5 @@ Discussion points:
|
||||
* Try replacing `drop(a)` with `a.drop()`.
|
||||
|
||||
</details>
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/ops/trait.Drop.html
|
||||
|
@ -1,6 +1,6 @@
|
||||
# `From` and `Into`
|
||||
|
||||
Types implement `From` and `Into` to facilitate type conversions:
|
||||
Types implement [`From`][1] and [`Into`][2] to facilitate type conversions:
|
||||
|
||||
```rust,editable
|
||||
fn main() {
|
||||
@ -12,7 +12,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
`Into` is automatically implemented when `From` is implemented:
|
||||
[`Into`][2] is automatically implemented when [`From`][1] is implemented:
|
||||
|
||||
```rust,editable
|
||||
fn main() {
|
||||
@ -31,3 +31,6 @@ fn main() {
|
||||
Your function will accept types that implement `From` and those that _only_ implement `Into`.
|
||||
|
||||
</details>
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/convert/trait.From.html
|
||||
[2]: https://doc.rust-lang.org/std/convert/trait.Into.html
|
||||
|
@ -1,6 +1,6 @@
|
||||
# FromIterator
|
||||
|
||||
`FromIterator` lets you build a collection from an `Iterator`.
|
||||
[`FromIterator`][1] lets you build a collection from an [`Iterator`][2].
|
||||
|
||||
```rust,editable
|
||||
fn main() {
|
||||
@ -24,3 +24,6 @@ There are also implementations which let you do cool things like convert an
|
||||
`Iterator<Item = Result<V, E>>` into a `Result<Vec<V>, E>`.
|
||||
|
||||
</details>
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/iter/trait.FromIterator.html
|
||||
[2]: https://doc.rust-lang.org/std/iter/trait.Iterator.html
|
||||
|
@ -2,9 +2,20 @@
|
||||
|
||||
We will now look at some of the most common traits of the Rust standard library:
|
||||
|
||||
* `Iterator` and `IntoIterator` used in `for` loops,
|
||||
* `From` and `Into` used to convert values,
|
||||
* `Read` and `Write` used for IO,
|
||||
* `Add`, `Mul`, ... used for operator overloading, and
|
||||
* `Drop` used for defining destructors.
|
||||
* `Default` used to construct a default instance of a type.
|
||||
* [`Iterator`][1] and [`IntoIterator`][2] used in `for` loops,
|
||||
* [`From`][3] and [`Into`][4] used to convert values,
|
||||
* [`Read`][5] and [`Write`][6] used for IO,
|
||||
* [`Add`][7], [`Mul`][8], ... used for operator overloading, and
|
||||
* [`Drop`][9] used for defining destructors.
|
||||
* [`Default`][10] used to construct a default instance of a type.
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/iter/trait.Iterator.html
|
||||
[2]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html
|
||||
[3]: https://doc.rust-lang.org/std/convert/trait.From.html
|
||||
[4]: https://doc.rust-lang.org/std/convert/trait.Into.html
|
||||
[5]: https://doc.rust-lang.org/std/io/trait.Read.html
|
||||
[6]: https://doc.rust-lang.org/std/io/trait.Write.html
|
||||
[7]: https://doc.rust-lang.org/std/ops/trait.Add.html
|
||||
[8]: https://doc.rust-lang.org/std/ops/trait.Mul.html
|
||||
[9]: https://doc.rust-lang.org/std/ops/trait.Drop.html
|
||||
[10]: https://doc.rust-lang.org/std/default/trait.Default.html
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Iterators
|
||||
|
||||
You can implement the `Iterator` trait on your own types:
|
||||
You can implement the [`Iterator`][1] trait on your own types:
|
||||
|
||||
```rust,editable
|
||||
struct Fibonacci {
|
||||
@ -37,3 +37,5 @@ fn main() {
|
||||
implementations.
|
||||
|
||||
</details>
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/iter/trait.Iterator.html
|
||||
|
@ -1,6 +1,6 @@
|
||||
# `Add`, `Mul`, ...
|
||||
|
||||
Operator overloading is implemented via traits in `std::ops`:
|
||||
Operator overloading is implemented via traits in [`std::ops`][1]:
|
||||
|
||||
```rust,editable
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
@ -36,3 +36,5 @@ Discussion points:
|
||||
trait.
|
||||
|
||||
</details>
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/ops/index.html
|
||||
|
@ -1,6 +1,6 @@
|
||||
# `Read` and `Write`
|
||||
|
||||
Using `Read` and `BufRead`, you can abstract over `u8` sources:
|
||||
Using [`Read`][1] and [`BufRead`][2], you can abstract over `u8` sources:
|
||||
|
||||
```rust,editable
|
||||
use std::io::{BufRead, BufReader, Read, Result};
|
||||
@ -20,7 +20,7 @@ fn main() -> Result<()> {
|
||||
}
|
||||
```
|
||||
|
||||
Similarly, `Write` lets you abstract over `u8` sinks:
|
||||
Similarly, [`Write`][3] lets you abstract over `u8` sinks:
|
||||
|
||||
```rust,editable
|
||||
use std::io::{Result, Write};
|
||||
@ -38,3 +38,7 @@ fn main() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/io/trait.Read.html
|
||||
[2]: https://doc.rust-lang.org/std/io/trait.BufRead.html
|
||||
[3]: https://doc.rust-lang.org/std/io/trait.Write.html
|
||||
|
Loading…
x
Reference in New Issue
Block a user