3 ms·
Here's an example of how the behaviour between Rust and Python differ because of scoping: in Rust: fn main() { let a = 3; let f = | | { a }; let
by gopiandcode 5y ago
Here's an example of how the behaviour between Rust and Python differ because of scoping:
in Rust:
fn main() {
let a = 3;
let f = | | { a };
let a = "b";
println!("{}, {}", f(), a);
}
in Python:
a = 3
def f ():
return a
a = "b"
print("{}, {}".format(f(), a))
The rust code outputs "3, b" while the python code outputs "b, b". The python code _assigns_ to the variable a, whereas the rust code creates a new scope and redefines a, shadowing it in the parent scope.
- KingOfCoders 5y agoYes you're right, I also would think closures would be impacted by scope.