by KingOfCoders · 5y ago · 0 comments
fn main() {
let a = String::from("Hallo");
let a = 3;
}I could then call
a = "hallo"
// can call String methods, runtime checked
a = 3
// can call int methods, runtime checked
in Python also variable shadowing. But in this case you assume a is the same but the type changes, and call it dynamic typing, while in the Rust example you assume the two a's are not the same variable: let a = String::from("Hallo");
// can call String methods, compiler checked
let a = 3
// can call i methods, compiler checked
One could argue there is a difference with method signatures def f( a )
a = "hallo"
f(a)
a = 3
f(a)
which does work in Python ("dynamic") but not in Rust ("static").But then the actual usage of "a" in the method effectively puts it on the same level as interfaces in Rust and blurs further with static languages like Typescript without nominal types.
You could, but you would be wrong. Shadowing is associated with opening a new scope. Reassignment modifies a location in an existing scope. Python does the latter.
a = "hello"
{
// new scope
a = 3
}
// a == "hello"
and is there a difference between Rust and Python?You can read more about what things the programmer has to manually avoid while writing code in "unsafe" code blocks: https://doc.rust-lang.org/reference/behavior-considered-unde...
EDIT: this means that if you stick to safe rust, the type system ensures you cannot have data races (among other things)