diff options
author | Jasper Ras <jras@hostnet.nl> | 2025-08-03 22:29:47 +0200 |
---|---|---|
committer | Jasper Ras <jras@hostnet.nl> | 2025-08-03 22:29:47 +0200 |
commit | 931eb0894e7cd3717f1218f0eb06382b11734c13 (patch) | |
tree | b6ad9b0d98a8f26e496c91ad3f24d1c855b654df /Shadowing variables.md | |
parent | af98004d2cf437603f47f4224110a80d76d0d7de (diff) |
vault backup: 2025-08-03 22:29:47
Diffstat (limited to 'Shadowing variables.md')
-rw-r--r-- | Shadowing variables.md | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Shadowing variables.md b/Shadowing variables.md new file mode 100644 index 0000000..ce10d66 --- /dev/null +++ b/Shadowing variables.md @@ -0,0 +1,12 @@ +[[Rust]] + +It is basically redeclaring the same variable, but it can be redeclared with a **different type**. A immutable variable can also be shadowed. + +Shadowing is undone when the scope ends, for example in a closure I can shadow a variable from the parent scope, but in the parent scope after the child scope ends we have the original value not the one from the child scope. + +It is useful for reusing a name, ie: +```rust +let spaces = " "; +let spaces = spaces.len(); +``` +> the type of spaces changes from string to u32. |