Question on this (section 3.5) page 44 sentence, please: "That block automatically gets everything from the parent scope."
Is the sentence true for blocks that are function bodies?
I modified the code on page 44, adding a let statement and a nested function:
fn say_apples (apples:i32) {
println!(" {}", apples);
let oranges = 2;
fn wont_compile() -> i32 {
apples + oranges
}
}
fn main() {
say_apples(10);
//println!("apples == {}", apples);
}
The function wont_compile() gets 2 occurrences of
----> error[E0434]: can't capture dynamic environment in a fn item <----
once for apples, once for oranges.
I don't mean to force my opinion on you or others who read this post. That said, I really, really like error E0434. It forces Rust programmers to put all named values such as apples/oranges in the function's parameter list. And since we only write short functions, that means when the function's source code is re-read, re-read, and re-read again during maintenance, the total life-cycle cost of writing once and re-reading many times is lower, because all the names the re-reading coders might wonder about are *right there* in the parameter list.
I'm pretty new to Rust. Have I misunderstood about error E0434?
ps - Really enjoying the book. Retired software engineer looking at books to use to teach coding to grandchildren when they get older.