1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-11 13:18:43 +02:00

Update methods.md (#247)

Adding a Q/A about `impl<T> Point<T>`, why is it specified twice.
This commit is contained in:
Igor Petruk 2023-01-23 13:38:53 +00:00 committed by GitHub
parent 05cc2ff480
commit b75e713792
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,3 +19,13 @@ fn main() {
println!("p.x = {}", p.x());
}
```
<details>
* *Q:* Why `T` is specified twice in `impl<T> Point<T> {}`? Isn't that redundant?
* This is because it is a generic implementation section for generic type. They are independently generic.
* It means these methods are defined for any `T`.
* It is possible to write `impl Point<u32> { .. }`.
* `Point` is still generic and you can use `Point<f64>`, but methods in this block will only be available for `Point<u32>`.
</details>