diff --git a/book/src/development/trait_checking.md b/book/src/development/trait_checking.md index 4944970dd55..08a6c40e90d 100644 --- a/book/src/development/trait_checking.md +++ b/book/src/development/trait_checking.md @@ -96,7 +96,11 @@ impl LateLintPass<'_> for CheckTokioAsyncReadExtTrait { ## Creating traits programmatically -Traits are often generic over a type e.g. `Borrow` is generic over `T`, and rust allows us to implement a trait for a specific type. For example, we can implement `Borrow` 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: +Traits are often generic over a type e.g. `Borrow` is generic over `T`, and rust allows us to implement a trait for +a specific type. For example, we can implement `Borrow` 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: ```rust @@ -111,7 +115,9 @@ if implements_trait(cx, ty, borrow_id, &[Ty::new_slice(cx.tcx, cx.tcx.types.u8). } ``` -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. +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. diff --git a/book/src/development/type_checking.md b/book/src/development/type_checking.md index ed85e416475..42e84a3d0f5 100644 --- a/book/src/development/type_checking.md +++ b/book/src/development/type_checking.md @@ -125,11 +125,15 @@ the [`TypeckResults::node_type()`][node_type] method inside of bodies. ## Creating Types programmatically -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. +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 a bit here. -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. +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. -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. +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. Here's an example of how to create a `Ty` for a slice of `u8`, i.e. `[u8]`