Why does Rust have unique anonymous types and how to work with them?
Hello everyone! I recently started learning Rust and I'm having trouble understanding the concept of anonymous types. I read that in Rust, each closure literal has its own unique, unnameable type. For example, this code:
```rust
let add_one = |x: i32| x + 1;
let double = |x: i32| x * 2;
```
I understand that `add_one` and `double` are different closures with different types. But why is this done? In C++ or other languages, I can simply declare a function pointer or use `std::function`. Why did Rust go down the path of creating unique anonymous types? It seems to me that this complicates writing generic code, especially when I want to pass a closure to a function or store it in a struct. For example, I can't just write:
```rust
fn apply(f: ???, x: i32) -> i32 {
f(x)
}
```
No answers yet.
Log in to leave an answer