Re:Grain: A strongly-typed functional programming language f...
by gopiandcode on Monday April 26, 2021 @06:52AM
(#26941200)
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.
in Rust:
in Python: 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.