|
12 | 12 |
|
13 | 13 | #![stable(feature = "rust1", since = "1.0.0")]
|
14 | 14 |
|
15 |
| -// impl Borrow<str> for String |
16 |
| -// impl<T> Borrow<T> for Arc<T> |
17 |
| -// impl<K> HashSet<K> { fn get<Q>(&self, q: &Q) where K: Borrow<Q> } |
18 |
| - |
19 | 15 | /// A trait identifying how borrowed data behaves.
|
20 | 16 | ///
|
21 | 17 | /// If a type implements this trait, it signals that a reference to it behaves
|
|
26 | 22 | ///
|
27 | 23 | /// As a consequence, this trait should only be implemented for types managing
|
28 | 24 | /// a value of another type without modifying its behavior. Examples are
|
29 |
| -/// smart pointers such as [`Box`] or [`Rc`] as well the owned version of |
30 |
| -/// slices such as [`Vec`]. |
| 25 | +/// smart pointers such as [`Box<T>`] or [`Rc<T>`] as well the owned version |
| 26 | +/// of slices such as [`Vec<T>`]. |
31 | 27 | ///
|
32 |
| -/// A relaxed version that allows providing a reference to some other type |
| 28 | +/// A relaxed version that allows converting a reference to some other type |
33 | 29 | /// without any further promises is available through [`AsRef`].
|
34 | 30 | ///
|
35 | 31 | /// When writing generic code, a use of `Borrow` should always be justified
|
|
41 | 37 | /// The companion trait [`BorrowMut`] provides the same guarantees for
|
42 | 38 | /// mutable references.
|
43 | 39 | ///
|
44 |
| -/// [`Box`]: ../boxed/struct.Box.html |
45 |
| -/// [`Rc`]: ../rc/struct.Rc.html |
46 |
| -/// [`Vec`]: ../vec/struct.Vec.html |
| 40 | +/// [`Box<T>`]: ../boxed/struct.Box.html |
| 41 | +/// [`Rc<T>`]: ../rc/struct.Rc.html |
| 42 | +/// [`Vec<T>`]: ../vec/struct.Vec.html |
47 | 43 | /// [`AsRef`]: ../convert/trait.AsRef.html
|
48 | 44 | /// [`BorrowMut`]: trait.BorrowMut.html
|
49 | 45 | ///
|
50 | 46 | /// # Examples
|
51 | 47 | ///
|
52 |
| -/// As a data collection, [`HashMap`] owns both keys and values. If the key’s |
53 |
| -/// actual data is wrapped in a managing type of some kind, it should, |
54 |
| -/// however, still be possible to search for a value using a reference to the |
55 |
| -/// key’s data. For instance, if the key is a string, then it is likely |
56 |
| -/// stored with the hash map as a [`String`], while it should be possible |
57 |
| -/// to search using a [`&str`][`str`]. Thus, `insert` needs to operate on a |
58 |
| -/// string while `get` needs to be able to use a `&str`. |
| 48 | +/// As a data collection, [`HashMap<K, V>`] owns both keys and values. If |
| 49 | +/// the key’s actual data is wrapped in a managing type of some kind, it |
| 50 | +/// should, however, still be possible to search for a value using a |
| 51 | +/// reference to the key’s data. For instance, if the key is a string, then |
| 52 | +/// it is likely stored with the hash map as a [`String`], while it should |
| 53 | +/// be possible to search using a [`&str`][`str`]. Thus, `insert` needs to |
| 54 | +/// operate on a `String` while `get` needs to be able to use a `&str`. |
59 | 55 | ///
|
60 |
| -/// Slightly simplified, the relevant parts of `HashMap` look like this: |
| 56 | +/// Slightly simplified, the relevant parts of `HashMap<K, V>` look like |
| 57 | +/// this: |
61 | 58 | ///
|
62 | 59 | /// ```
|
63 | 60 | /// use std::borrow::Borrow;
|
|
70 | 67 | ///
|
71 | 68 | /// impl<K, V> HashMap<K, V> {
|
72 | 69 | /// pub fn insert(&self, key: K, value: V) -> Option<V>
|
73 |
| -/// where K: Hash + Eq |
| 70 | +/// where K: Hash + Eq |
74 | 71 | /// {
|
75 | 72 | /// # unimplemented!()
|
76 | 73 | /// // ...
|
77 | 74 | /// }
|
78 | 75 | ///
|
79 | 76 | /// pub fn get<Q>(&self, k: &Q) -> Option<&V>
|
80 |
| -/// where K: Borrow<Q>, |
81 |
| -/// Q: Hash + Eq + ?Sized |
| 77 | +/// where |
| 78 | +/// K: Borrow<Q>, |
| 79 | +/// Q: Hash + Eq + ?Sized |
82 | 80 | /// {
|
83 | 81 | /// # unimplemented!()
|
84 | 82 | /// // ...
|
85 | 83 | /// }
|
86 | 84 | /// }
|
87 | 85 | /// ```
|
88 | 86 | ///
|
89 |
| -/// The entire hash map is generic over the stored type for the key, `K`. |
90 |
| -/// When inserting a value, the map is given such a `K` and needs to find |
91 |
| -/// the correct hash bucket and check if the key is already present based |
92 |
| -/// on that `K` value. It therefore requires `K: Hash + Eq`. |
| 87 | +/// The entire hash map is generic over a key type `K`. Because these keys |
| 88 | +/// are stored by with the hash map, this type as to own the key’s data. |
| 89 | +/// When inserting a key-value pair, the map is given such a `K` and needs |
| 90 | +/// to find the correct hash bucket and check if the key is already present |
| 91 | +/// based on that `K`. It therefore requires `K: Hash + Eq`. |
93 | 92 | ///
|
94 | 93 | /// In order to search for a value based on the key’s data, the `get` method
|
95 | 94 | /// is generic over some type `Q`. Technically, it needs to convert that `Q`
|
|
103 | 102 | /// result as `Q`’s by demanding that `K: Borrow<Q>`.
|
104 | 103 | ///
|
105 | 104 | /// As a consequence, the hash map breaks if a `K` wrapping a `Q` value
|
106 |
| -/// produces a different hash than `Q`. For instance, image you have a |
107 |
| -/// type that wraps a string but compares ASCII letters case-insensitive: |
| 105 | +/// produces a different hash than `Q`. For instance, imagine you have a |
| 106 | +/// type that wraps a string but compares ASCII letters ignoring their case: |
108 | 107 | ///
|
109 | 108 | /// ```
|
| 109 | +/// # #[allow(unused_imports)] |
110 | 110 | /// use std::ascii::AsciiExt;
|
111 | 111 | ///
|
112 | 112 | /// pub struct CIString(String);
|
|
121 | 121 | /// ```
|
122 | 122 | ///
|
123 | 123 | /// Because two equal values need to produce the same hash value, the
|
124 |
| -/// implementation of `Hash` need to reflect that, too: |
| 124 | +/// implementation of `Hash` needs to reflect that, too: |
125 | 125 | ///
|
126 | 126 | /// ```
|
127 |
| -/// # use std::ascii::AsciiExt; |
| 127 | +/// # #[allow(unused_imports)] use std::ascii::AsciiExt; |
128 | 128 | /// # use std::hash::{Hash, Hasher};
|
129 | 129 | /// # pub struct CIString(String);
|
130 | 130 | /// impl Hash for CIString {
|
|
145 | 145 | /// which doesn’t carry any such restrictions.
|
146 | 146 | ///
|
147 | 147 | /// [`Hash`]: ../hash/trait.Hash.html
|
148 |
| -/// [`HashMap`]: ../collections/struct.HashMap.html |
| 148 | +/// [`HashMap<K, V>`]: ../collections/struct.HashMap.html |
149 | 149 | /// [`String`]: ../string/struct.String.html
|
150 | 150 | /// [`str`]: ../primitive.str.html
|
151 | 151 | ///
|
|
0 commit comments