Skip to content

Commit f89d8d1

Browse files
committed
Auto merge of #42311 - bjorn3:patch-1, r=frewsxcv
Syntax highlight all rust code in librustc/traits/README.md Also replace `...` with `/*...*/`
2 parents e1fe1a8 + 681d97f commit f89d8d1

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/librustc/traits/README.md

+31-19
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ things.
88
Trait resolution is the process of pairing up an impl with each
99
reference 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

1315
and 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

1721
it is the job of trait resolution to figure out (in which case)
1822
whether 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
2125
find a specific impl, but we can figure out that the caller must
2226
provide 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

3137
The line marked `(*)` is only legal if `T` (the type of `*e`)
3238
implements the `Clone` trait. Naturally, since we don't know what `T`
@@ -107,7 +113,7 @@ otherwise the result is considered ambiguous.
107113
This process is easier if we work through some examples. Consider
108114
the following trait:
109115

110-
```
116+
```rust
111117
trait Convert<Target> {
112118
fn convert(&self) -> Target;
113119
}
@@ -119,8 +125,8 @@ wanted to permit conversion between `isize` and `usize`, we might
119125
implement `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

126132
Now imagine there is some code like the following:
@@ -205,12 +211,14 @@ using the definition of *matching* given above.
205211

206212
Consider 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

215223
Clearly we can use methods offered by `A1`, `A2`, or `B` within the
216224
body 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

248256
Here 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

255265
After 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:
343353
how to deal with impl obligations. I'll work through a simple example
344354
here. Imagine we have the traits `Foo` and `Bar` and an associated impl:
345355

346-
```
356+
```rust
347357
trait 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
401411
we come to the conclusion that the only possible impl is this one,
402412
with def-id 22:
403413

404-
impl Foo<isize> for usize { ... } // Impl #22
414+
```rust
415+
impl Foo<isize> for usize { ... } // Impl #22
416+
```
405417

406418
We would then record in the cache `usize : Foo<%0> ==>
407419
ImplCandidate(22)`. Next we would confirm `ImplCandidate(22)`, which

0 commit comments

Comments
 (0)