mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-16 23:55:42 +02:00
parent
3726918b87
commit
709450bfad
@ -57,14 +57,14 @@ fn main() {
|
|||||||
val: u8,
|
val: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
pick(123, Foo { val: 7 }, Foo { val: 456 });
|
pick(false, Foo { val: 7 }, Foo { val: 99 });
|
||||||
```
|
```
|
||||||
|
|
||||||
- This is similar to C++ templates, but Rust partially compiles the generic
|
- This is similar to C++ templates, but Rust partially compiles the generic
|
||||||
function immediately, so that function must be valid for all types matching
|
function immediately, so that function must be valid for all types matching
|
||||||
the constraints. For example, try modifying `pick` to return `even + odd` if
|
the constraints. For example, try modifying `pick` to return `left + right` if
|
||||||
`n == 0`. Even if only the `pick` instantiation with integers is used, Rust
|
`cond` is false. Even if only the `pick` instantiation with integers is used,
|
||||||
still considers it invalid. C++ would let you do this.
|
Rust still considers it invalid. C++ would let you do this.
|
||||||
|
|
||||||
- Generic code is turned into non-generic code based on the call sites. This is
|
- Generic code is turned into non-generic code based on the call sites. This is
|
||||||
a zero-cost abstraction: you get exactly the same result as if you had
|
a zero-cost abstraction: you get exactly the same result as if you had
|
||||||
|
Loading…
x
Reference in New Issue
Block a user