1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-04 03:25:08 +02:00

Add example for underscore syntax with generics (type-inference.md)

This commit is contained in:
Fabian Bornhofen 2023-01-10 11:30:48 +01:00
parent 2ec7bdf15d
commit b1069f863c

View File

@ -20,3 +20,23 @@ fn main() {
// takes_u32(y);
}
```
<details>
This slide demonstrates how the Rust compiler infers types based on constraints given by variable declarations and usages.
The following code tells the compiler to copy into a certain generic container without the code ever explicitly specifying the contained type, using `_` as a placeholder:
```rust,editable
fn main() {
let mut v = Vec::new();
v.push((10, false));
v.push((20, true));
println!("v: {v:?}");
let vv = v.iter().collect::<std::collections::HashSet<_>>();
println!("vv: {vv:?}");
}
```
</details>