Skip to content

Commit 58d9c47

Browse files
formatting
1 parent a6fc64b commit 58d9c47

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

book/src/development/trait_checking.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ impl LateLintPass<'_> for CheckTokioAsyncReadExtTrait {
9696

9797
## Creating traits programmatically
9898

99-
Traits are often generic over a type e.g. `Borrow<T>` is generic over `T`, and rust allows us to implement a trait for a specific type. For example, we can implement `Borrow<str>` for a hypothetical type `Foo`. Let's suppose that we would like to find whether our type actually implements `Borrow<[u8]>`. To do so, we need to supply a type that represents `[u8]`, but `[u8]` is also a generic, it's a slice over `u8`. We can create this type using Ty::new_slice method. The following code demonstrates how to do this:
99+
Traits are often generic over a type e.g. `Borrow<T>` is generic over `T`, and rust allows us to implement a trait for
100+
a specific type. For example, we can implement `Borrow<str>` for a hypothetical type `Foo`. Let's suppose that we
101+
would like to find whether our type actually implements `Borrow<[u8]>`. To do so, we need to supply a type that
102+
represents `[u8]`, but `[u8]` is also a generic, it's a slice over `u8`. We can create this type using Ty::new_slice
103+
method. The following code demonstrates how to do this:
100104

101105
```rust
102106

@@ -111,7 +115,9 @@ if implements_trait(cx, ty, borrow_id, &[Ty::new_slice(cx.tcx, cx.tcx.types.u8).
111115
}
112116
```
113117

114-
Here, we use `Ty::new_slice` to create a type that represents `[T]` and supply `u8` as a type parameter, and then we go on normally with `implements_trait` function. The [Ty] struct allows us to create types programmatically, and it's useful when we need to create types that we can't obtain through the usual means.
118+
Here, we use `Ty::new_slice` to create a type that represents `[T]` and supply `u8` as a type parameter, and then we go
119+
on normally with `implements_trait` function. The [Ty] struct allows us to create types programmatically, and it's
120+
useful when we need to create types that we can't obtain through the usual means.
115121

116122

117123

book/src/development/type_checking.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,15 @@ the [`TypeckResults::node_type()`][node_type] method inside of bodies.
125125
126126
## Creating Types programmatically
127127

128-
A common usecase for creating types programmatically is when we want to check if a type implements a trait. We have a section on this in the [Trait Checking](trait_checking.md) chapter, but given the importance of this topic, we will also cover it here.
128+
A common usecase for creating types programmatically is when we want to check if a type implements a trait. We have
129+
a section on this in the [Trait Checking](trait_checking.md) chapter, but given the importance of this topic, we will
130+
also cover it a bit here.
129131

130-
When we refer to "type" in this context, we refer to `ty::Ty`. To create a `ty::Ty` programmatically, we rely on `Ty::new_*` methods. These methods create a `TyKind` and then wrap it in a `Ty` struct.
132+
When we refer to "type" in this context, we refer to `ty::Ty`. To create a `ty::Ty` programmatically, we rely on
133+
`Ty::new_*` methods. These methods create a `TyKind` and then wrap it in a `Ty` struct.
131134

132-
This means we have access to all the primitive types, such as `Ty::new_char`, `Ty::new_bool`, `Ty::new_int`, etc. We can also create more complex types, such as slices, tuples, and references.
135+
This means we have access to all the primitive types, such as `Ty::new_char`, `Ty::new_bool`, `Ty::new_int`, etc.
136+
We can also create more complex types, such as slices, tuples, and references.
133137

134138
Here's an example of how to create a `Ty` for a slice of `u8`, i.e. `[u8]`
135139

0 commit comments

Comments
 (0)