Skip to content

Commit 5bef034

Browse files
committed
Bring back the phrase 'borrowing as' for what Borrow does.
1 parent 44be054 commit 5bef034

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

src/libcore/borrow.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
#![stable(feature = "rust1", since = "1.0.0")]
1414

15-
/// A trait identifying how borrowed data behaves.
15+
/// A trait for borrowing data.
1616
///
1717
/// In Rust, it is common to provide different representations of a type for
1818
/// different use cases. For instance, storage location and management for a
@@ -24,40 +24,37 @@
2424
/// [`str`]. This requires keeping additional information unnecessary for a
2525
/// simple, immutable string.
2626
///
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>`].
3137
///
3238
/// Further, when providing implementations for additional traits, it needs
3339
/// to be considered whether they should behave identical to those of the
3440
/// 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.
4144
///
4245
/// If generic code merely needs to work for all types that can
4346
/// provide a reference to related type `T`, it is often better to use
4447
/// [`AsRef<T>`] as more types can safely implement it.
4548
///
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-
///
5449
/// [`AsRef<T>`]: ../../std/convert/trait.AsRef.html
55-
/// [`BorrowMut`]: trait.BorrowMut.html
50+
/// [`BorrowMut<T>`]: trait.BorrowMut.html
5651
/// [`Box<T>`]: ../../std/boxed/struct.Box.html
5752
/// [`Mutex<T>`]: ../../std/sync/struct.Mutex.html
5853
/// [`Rc<T>`]: ../../std/rc/struct.Rc.html
5954
/// [`str`]: ../../std/primitive.str.html
6055
/// [`String`]: ../../std/string/struct.String.html
56+
/// [`borrow`]: #tymethod.borrow
57+
///
6158
///
6259
/// # Examples
6360
///
@@ -113,10 +110,10 @@
113110
/// `str` is available.
114111
///
115112
/// 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
120117
/// results.
121118
///
122119
/// The implementation of `get` relies in particular on identical
@@ -141,7 +138,7 @@
141138
/// ```
142139
///
143140
/// 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:
145142
///
146143
/// ```
147144
/// # use std::hash::{Hash, Hasher};

0 commit comments

Comments
 (0)