|
12 | 12 |
|
13 | 13 | #![stable(feature = "rust1", since = "1.0.0")]
|
14 | 14 |
|
15 |
| -/// A trait identifying how borrowed data behaves. |
| 15 | +/// A trait for borrowing data. |
16 | 16 | ///
|
17 | 17 | /// In Rust, it is common to provide different representations of a type for
|
18 | 18 | /// different use cases. For instance, storage location and management for a
|
|
24 | 24 | /// [`str`]. This requires keeping additional information unnecessary for a
|
25 | 25 | /// simple, immutable string.
|
26 | 26 | ///
|
27 |
| -/// These types signal that they are a specialized representation of a basic |
28 |
| -/// type `T` by implementing `Borrow<T>`. The method `borrow` provides a way |
29 |
| -/// to convert a reference to the type into a reference to this basic type |
30 |
| -/// `T`. |
| 27 | +/// These types provide access to the underlying data through references |
| 28 | +/// to the type of that data. They are said to be ‘borrowed as’ that type. |
| 29 | +/// For instance, a [`Box<T>`] can be borrowed as `T` while a [`String`] |
| 30 | +/// can be borrowed as `str`. |
| 31 | +/// |
| 32 | +/// Types express that they can be borrowed as some type `T` by implementing |
| 33 | +/// `Borrow<T>`, providing a reference to a `T` in the trait’s |
| 34 | +/// [`borrow`] method. A type is free to borrow as several different types. |
| 35 | +/// If it wishes to mutably borrow as the type – allowing the underlying data |
| 36 | +/// to be modified, it can additionally implement [`BorrowMut<T>`]. |
31 | 37 | ///
|
32 | 38 | /// Further, when providing implementations for additional traits, it needs
|
33 | 39 | /// to be considered whether they should behave identical to those of the
|
34 | 40 | /// underlying type as a consequence of acting as a representation of that
|
35 |
| -/// underlying type. |
36 |
| -/// |
37 |
| -/// Generic code typically uses `Borrow<T>` when it not only needs access |
38 |
| -/// to a reference of the underlying type but relies on the identical |
39 |
| -/// behavior of these additional trait implementations. These traits are |
40 |
| -/// likely to appear as additional trait bounds. |
| 41 | +/// underlying type. Generic code typically uses `Borrow<T>` when it relies |
| 42 | +/// on the identical behavior of these additional trait implementations. |
| 43 | +/// These traits will likely appear as additional trait bounds. |
41 | 44 | ///
|
42 | 45 | /// If generic code merely needs to work for all types that can
|
43 | 46 | /// provide a reference to related type `T`, it is often better to use
|
44 | 47 | /// [`AsRef<T>`] as more types can safely implement it.
|
45 | 48 | ///
|
46 |
| -/// If a type implementing `Borrow<T>` also wishes to allow mutable access |
47 |
| -/// to the underlying type `T`, it can do so by implementing the companion |
48 |
| -/// trait [`BorrowMut`]. |
49 |
| -/// |
50 |
| -/// Note also that it is perfectly fine for a single type to have multiple |
51 |
| -/// implementations of `Borrow<T>` for different `T`s. In fact, a blanket |
52 |
| -/// implementation lets every type be at least a borrow of itself. |
53 |
| -/// |
54 | 49 | /// [`AsRef<T>`]: ../../std/convert/trait.AsRef.html
|
55 |
| -/// [`BorrowMut`]: trait.BorrowMut.html |
| 50 | +/// [`BorrowMut<T>`]: trait.BorrowMut.html |
56 | 51 | /// [`Box<T>`]: ../../std/boxed/struct.Box.html
|
57 | 52 | /// [`Mutex<T>`]: ../../std/sync/struct.Mutex.html
|
58 | 53 | /// [`Rc<T>`]: ../../std/rc/struct.Rc.html
|
59 | 54 | /// [`str`]: ../../std/primitive.str.html
|
60 | 55 | /// [`String`]: ../../std/string/struct.String.html
|
| 56 | +/// [`borrow`]: #tymethod.borrow |
| 57 | +/// |
61 | 58 | ///
|
62 | 59 | /// # Examples
|
63 | 60 | ///
|
|
113 | 110 | /// `str` is available.
|
114 | 111 | ///
|
115 | 112 | /// Instead, the `get` method is generic over the type of the underlying key
|
116 |
| -/// data, called `Q` in the method signature above. It states that `K` is a |
117 |
| -/// representation of `Q` by requiring that `K: Borrow<Q>`. By additionally |
118 |
| -/// requiring `Q: Hash + Eq`, it demands that `K` and `Q` have |
119 |
| -/// implementations of the `Hash` and `Eq` traits that produce identical |
| 113 | +/// data, called `Q` in the method signature above. It states that `K` |
| 114 | +/// borrows as a `Q` by requiring that `K: Borrow<Q>`. By additionally |
| 115 | +/// requiring `Q: Hash + Eq`, it signals the requirement that `K` and `Q` |
| 116 | +/// have implementations of the `Hash` and `Eq` traits that produce identical |
120 | 117 | /// results.
|
121 | 118 | ///
|
122 | 119 | /// The implementation of `get` relies in particular on identical
|
|
141 | 138 | /// ```
|
142 | 139 | ///
|
143 | 140 | /// Because two equal values need to produce the same hash value, the
|
144 |
| -/// implementation of `Hash` needs to reflect that, too: |
| 141 | +/// implementation of `Hash` needs to ignore ASCII case, too: |
145 | 142 | ///
|
146 | 143 | /// ```
|
147 | 144 | /// # use std::hash::{Hash, Hasher};
|
|
0 commit comments