What do you think about rust let allowing redeclaration for assignment and assignment for type conversion?

What do you think about rust let allowing redeclaration for assignment and assignment for type conversion?

Have you learned a painful lesson from this?

Could you elaborate a bit what do you mean by assignment for type conversion? And what painful lesson?

Are you referring to variable shadowing?

1 Like

If you are talking about things like:

    let temperature = "1234".to_string();
    let temperature = temperature.parse::<u32>()?;

I love it.

It means I can change the type of some data in my program and not have to worry about thinking up a new name for it.

Rust is fussy about types so the worries about "shadowing" we might have in other languages don't really apply. Any programmer error made with it will show up immediately.

6 Likes

If so, if you don't design it properly or think it through, the variable will not be able to store data correctly, because it can easily be initialized and reset by accident.

I don't understand what you mean. Could you give an example of where shadowing is problematic in Rust?