@@ -8,11 +8,15 @@ things.
88Trait resolution is the process of pairing up an impl with each
99reference to a trait. So, for example, if there is a generic function like:
1010
11- fn clone_slice<T:Clone>(x: &[T]) -> Vec<T> { ... }
11+ ``` rust
12+ fn clone_slice <T : Clone >(x : & [T ]) -> Vec <T > { /*...*/ }
13+ ```
1214
1315and then a call to that function:
1416
15- let v: Vec<isize> = clone_slice([1, 2, 3])
17+ ``` rust
18+ let v : Vec <isize > = clone_slice (& [1 , 2 , 3 ])
19+ ```
1620
1721it is the job of trait resolution to figure out (in which case)
1822whether there exists an impl of ` isize : Clone `
@@ -21,12 +25,14 @@ Note that in some cases, like generic functions, we may not be able to
2125find a specific impl, but we can figure out that the caller must
2226provide an impl. To see what I mean, consider the body of ` clone_slice ` :
2327
24- fn clone_slice<T:Clone>(x: &[T]) -> Vec<T> {
25- let mut v = Vec::new();
26- for e in &x {
27- v.push((*e).clone()); // (*)
28- }
28+ ``` rust
29+ fn clone_slice < T : Clone >( x : & [ T ]) -> Vec < T > {
30+ let mut v = Vec :: new ();
31+ for e in & x {
32+ v . push (( * e ) . clone ()); // (*)
2933 }
34+ }
35+ ```
3036
3137The line marked ` (*) ` is only legal if ` T ` (the type of ` *e ` )
3238implements the ` Clone ` trait. Naturally, since we don't know what ` T `
@@ -107,7 +113,7 @@ otherwise the result is considered ambiguous.
107113This process is easier if we work through some examples. Consider
108114the following trait:
109115
110- ```
116+ ``` rust
111117trait Convert <Target > {
112118 fn convert (& self ) -> Target ;
113119}
@@ -119,8 +125,8 @@ wanted to permit conversion between `isize` and `usize`, we might
119125implement ` Convert ` like so:
120126
121127``` rust
122- impl Convert <usize > for isize { ... } // isize -> usize
123- impl Convert <isize > for usize { ... } // usize -> isize
128+ impl Convert <usize > for isize { /* ...*/ } // isize -> usize
129+ impl Convert <isize > for usize { /* ...*/ } // usize -> isize
124130```
125131
126132Now imagine there is some code like the following:
@@ -205,12 +211,14 @@ using the definition of *matching* given above.
205211
206212Consider this simple example:
207213
208- trait A1 { ... }
209- trait A2 : A1 { ... }
214+ ``` rust
215+ trait A1 { /*...*/ }
216+ trait A2 : A1 { /*...*/ }
210217
211- trait B { ... }
218+ trait B { /* ...*/ }
212219
213- fn foo<X:A2+B> { ... }
220+ fn foo <X : A2 + B > { /*...*/ }
221+ ```
214222
215223Clearly we can use methods offered by ` A1 ` , ` A2 ` , or ` B ` within the
216224body of ` foo ` . In each case, that will incur an obligation like `X :
@@ -247,10 +255,12 @@ to us, so we must run trait selection to figure everything out.
247255
248256Here is an example:
249257
250- trait Foo { ... }
251- impl<U,T:Bar<U>> Foo for Vec<T> { ... }
258+ ``` rust
259+ trait Foo { /*...*/ }
260+ impl <U ,T : Bar <U >> Foo for Vec <T > { /*...*/ }
252261
253- impl Bar<usize> for isize { ... }
262+ impl Bar <usize > for isize { /*...*/ }
263+ ```
254264
255265After one shallow round of selection for an obligation like `Vec<isize >
256266: Foo`, we would know which impl we want, and we would know that
@@ -343,7 +353,7 @@ Once the basic matching is done, we get to another interesting topic:
343353how to deal with impl obligations. I'll work through a simple example
344354here. Imagine we have the traits ` Foo ` and ` Bar ` and an associated impl:
345355
346- ```
356+ ``` rust
347357trait Foo <X > {
348358 fn foo (& self , x : X ) { }
349359}
@@ -401,7 +411,9 @@ Therefore, we search through impls and where clauses and so forth, and
401411we come to the conclusion that the only possible impl is this one,
402412with def-id 22:
403413
404- impl Foo<isize> for usize { ... } // Impl #22
414+ ``` rust
415+ impl Foo <isize > for usize { ... } // Impl #22
416+ ```
405417
406418We would then record in the cache `usize : Foo<%0> ==>
407419ImplCandidate(22)` . Next we would confirm ` ImplCandidate(22)`, which
0 commit comments