# Rust let is a point of failure

**URL:** <https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821>\
**Category:** language design\
**Created:** [November 5, 2024, 1:19am UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821 "2024-11-05T01:19:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Super1Windcloud](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/super1windcloud/32/12696_2.png) [@Super1Windcloud](https://internals.rust-lang.org/u/Super1Windcloud)\
**Post date:** [November 5, 2024, 1:19am UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/1 "2024-11-05T01:19:57Z")

</div>

Today, I encountered a problem when writing code. An encapsulated function processes logic, but the function is cyclic outside, and there is a let mut hashmap definition inside the function. This causes the hashmap to be reinitialized every time it is looped, causing deviations from expectations. As a result, I think that let should not allow variable shadowing. For example, go's var, Typescript/JavaScript's let, and const all do not allow variable shadowing. Why is rust like this?

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [November 5, 2024, 2:45am UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/2 "2024-11-05T02:45:29Z")

</div>

Can you give a code example of the issue you encountered? It's hard to tell by just your description.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [November 5, 2024, 2:53am UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/3 "2024-11-05T02:53:07Z")

</div>

> [@Super1Windcloud](#):
>
> JavaScript's let, and const all do not allow variable shadowing

Unityped dynamic languages are completely different when it comes to how scoping works, so that comparison is not useful.

Rust has a compiler to give you "hey, you never edited that outer one" warnings, and you can't do things like `x = x.to_string();` in rust -- the way you can in JS that doesn't care -- so shadowing is useful in Rust in a way it isn't in JS.

---

<div class="post-metadata">

**Author:** ![matt1985](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matt1985/32/4772_2.png) [@matt1985](https://internals.rust-lang.org/u/matt1985)\
**Post date:** [November 5, 2024, 7:10am UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/4 "2024-11-05T07:10:57Z")

</div>

Clippy has lints for shadowing, the most useful one being the [`clippy::shadow_unrelated`](https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated) lint, which prevents accidental shadowing, while allowing idiomatic shadowing patterns.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [November 5, 2024, 7:22am UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/5 "2024-11-05T07:22:31Z")

</div>

Please remove the "Unsafe Code Guidelines" tag from this thread, it has nothing to do with the UCG.

---

<div class="post-metadata">

**Author:** ![jvcmarcenes](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jvcmarcenes/32/11971_2.png) [@jvcmarcenes](https://internals.rust-lang.org/u/jvcmarcenes)\
**Post date:** [November 10, 2024, 5:51pm UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/6 "2024-11-10T17:51:20Z")

</div>

> [@Super1Windcloud](#):
>
> An encapsulated function processes logic, but the function is cyclic outside

Then pass the hash map as a parameter instead of constructing it in the function?

That's a common pattern, std does it for Strings and Vecs.

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [November 10, 2024, 7:03pm UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/7 "2024-11-10T19:03:11Z")

</div>

> [@Super1Windcloud](#):
>
> As a result, I think that let should not allow variable shadowing.

I'm afraid that ship has sailed. Changing these kinds of rules retroactively means breaking real, extant code, be in on github somewhere or in proprietary code. That in turn would conflict with Rust's stability guarantees.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [November 10, 2024, 11:26pm UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/8 "2024-11-10T23:26:14Z")

</div>

In Rust, existence of variables is semantically meaningful.

In JS code like this:

```js
let string = example.toLowerCase().trim();

```

is always the same as:

```js
let tmp = example.toLowerCase();
let string = tmp.trim();

```

but in Rust, these two examples mean different things.

```rust
let string = example.to_lowercase().trim(); // ERROR!!!

```

```rust
let string = example.to_lowercase();
let string = string.trim(); // OK

```

In Rust you may need to break an expression into multiple parts, to ensure the temporary intermediate results are kept.

JS is dynamically typed, and doesn't have borrowed vs owned distinction, so if you want, you can reuse the same variable for multiple steps of a computation:

```js
let string = example.toLowerCase();
string = string.trim();

```

but in Rust it's not always possible when the types change:

```rust
let mut string = example.to_lowercase();
string = string.trim(); // ERROR!!!

```

which makes shadowing useful, because you can shadow a variable with the same name but a different type, which is almost as convenient as dynamically typed variables. And you don't have to create `string1`, `string2`, `string3`, and keep track of which one is the latest one.

---

<div class="post-metadata">

**Author:** ![bert](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@bert](https://internals.rust-lang.org/u/bert)\
**Post date:** [November 11, 2024, 5:25am UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/9 "2024-11-11T05:25:22Z")

</div>

Is this code unidiomatic? I took it [straight from the docs](https://doc.rust-lang.org/std/fs/fn.read_dir.html).

```rust
for entry in fs::read_dir(dir)? {
    let entry = entry?;
    // ...
}

```

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simonbuchan/32/9390_2.png) [@simonbuchan](https://internals.rust-lang.org/u/simonbuchan)\
**Post date:** [November 11, 2024, 8:18pm UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/10 "2024-11-11T20:18:09Z")

</div>

> [@Super1Windcloud](#):
>
> For example, go's var, Typescript/JavaScript's let, and const all do not allow variable shadowing

... What? Yes they do, they just require a block in between, and a loop counts as a block - from what I could understand of the code you're describing this would be identical in JavaScript (which is rare to be able to say in Rust!).

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [May 5, 2026, 8:18pm UTC](https://internals.rust-lang.org/t/rust-let-is-a-point-of-failure/21821/11 "2026-05-05T20:18:13Z")

</div>

Closed on May 5, 2026, 8:18pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
