diff options
| author | Quinn Sinclair <me@partiallytyped.dev> | 2024-02-13 01:36:54 +0100 |
|---|---|---|
| committer | Quinn Sinclair <me@partiallytyped.dev> | 2024-03-03 18:12:41 +0100 |
| commit | 58d9c4707e11df900a4ecca5522d51a87f889224 (patch) | |
| tree | 4b2b923e7bed88cb6e1c362098042d758b237526 | |
| parent | a6fc64bb7b4c2bda431a95af25b8c380a1c78575 (diff) | |
| download | rust-58d9c4707e11df900a4ecca5522d51a87f889224.tar.gz rust-58d9c4707e11df900a4ecca5522d51a87f889224.zip | |
formatting
| -rw-r--r-- | book/src/development/trait_checking.md | 10 | ||||
| -rw-r--r-- | book/src/development/type_checking.md | 10 |
2 files changed, 15 insertions, 5 deletions
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<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: +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: ```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]` |
