I'm currently reading the Rust book, and I have just reached the topic closures
.
A detail that has surprised me, is that the Rust book sais that
Closures don’t require you to annotate the types of the parameters
I immeadiatly tested that, since it appeared really counter-intuitive to how Rust usually works. Thus, i copied exactly the closure they used, pasted it into my code, and... got an error:
fn some_closure() {
let expensive_closure = |num| {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
num
};
}
error[E0282]: type annotations needed
--> src/main.rs:14:30
|
14 | let expensive_closure = |num| {
| ^^^ consider giving this closure parameter a type
error: aborting due to previous error
I sure know the meaning of that error, still I am confused by it, since not only the book, but also the reference specify that theres no annotations needed, yet I am getting this error.
Is this just a recent change that hasn't been documented yet, or is there something I misunderstand?
num
is used in absolutely no way. So there's absolutely no way to determine its type. Did you want to usenum
instead of2
in the closure ? Is that a typo ? – Denys Séguret