]> git.proxmox.com Git - rustc.git/blame - src/doc/book/borrow-and-asref.md
New upstream version 1.13.0+dfsg1
[rustc.git] / src / doc / book / borrow-and-asref.md
CommitLineData
bd371182
AL
1% Borrow and AsRef
2
3The [`Borrow`][borrow] and [`AsRef`][asref] traits are very similar, but
4different. Here’s a quick refresher on what these two traits mean.
5
6[borrow]: ../std/borrow/trait.Borrow.html
7[asref]: ../std/convert/trait.AsRef.html
8
9# Borrow
10
9e0c209e 11The `Borrow` trait is used when you’re writing a data structure, and you want to
bd371182
AL
12use either an owned or borrowed type as synonymous for some purpose.
13
14For example, [`HashMap`][hashmap] has a [`get` method][get] which uses `Borrow`:
15
16```rust,ignore
17fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
18 where K: Borrow<Q>,
19 Q: Hash + Eq
20```
21
22[hashmap]: ../std/collections/struct.HashMap.html
23[get]: ../std/collections/struct.HashMap.html#method.get
24
25This signature is pretty complicated. The `K` parameter is what we’re interested
26in here. It refers to a parameter of the `HashMap` itself:
27
28```rust,ignore
29struct HashMap<K, V, S = RandomState> {
30```
31
32The `K` parameter is the type of _key_ the `HashMap` uses. So, looking at
33the signature of `get()` again, we can use `get()` when the key implements
34`Borrow<Q>`. That way, we can make a `HashMap` which uses `String` keys,
35but use `&str`s when we’re searching:
36
37```rust
38use std::collections::HashMap;
39
40let mut map = HashMap::new();
41map.insert("Foo".to_string(), 42);
42
43assert_eq!(map.get("Foo"), Some(&42));
44```
45
46This is because the standard library has `impl Borrow<str> for String`.
47
48For most types, when you want to take an owned or borrowed type, a `&T` is
49enough. But one area where `Borrow` is effective is when there’s more than one
62682a34
SL
50kind of borrowed value. This is especially true of references and slices: you
51can have both an `&T` or a `&mut T`. If we wanted to accept both of these types,
52`Borrow` is up for it:
bd371182 53
62682a34 54```rust
bd371182
AL
55use std::borrow::Borrow;
56use std::fmt::Display;
57
58fn foo<T: Borrow<i32> + Display>(a: T) {
59 println!("a is borrowed: {}", a);
60}
61
62let mut i = 5;
63
64foo(&i);
65foo(&mut i);
66```
67
68This will print out `a is borrowed: 5` twice.
69
70# AsRef
71
72The `AsRef` trait is a conversion trait. It’s used for converting some value to
73a reference in generic code. Like this:
74
75```rust
76let s = "Hello".to_string();
77
78fn foo<T: AsRef<str>>(s: T) {
79 let slice = s.as_ref();
80}
81```
82
83# Which should I use?
84
85We can see how they’re kind of the same: they both deal with owned and borrowed
86versions of some type. However, they’re a bit different.
87
88Choose `Borrow` when you want to abstract over different kinds of borrowing, or
9e0c209e 89when you’re building a data structure that treats owned and borrowed values in
bd371182
AL
90equivalent ways, such as hashing and comparison.
91
92Choose `AsRef` when you want to convert something to a reference directly, and
93you’re writing generic code.