Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

  let foo = "...";
  let foo = parse(foo);
  let foo = escaped(foo);
Is it really shadowing or mutation of foo?

I would consider this shadowing (something you can do in OcaML):

  let foo = "..." in
    let foo = parse(foo) in
     let foo = escaped(foo) in
        dosomething(foo);;


It is shadowing (though it could be converted into mutation by a sufficiently smart compiler...). ReasonML does something similar with their take on Ocaml.

https://reasonml.github.io/docs/en/let-binding


The foos can all have different types which is not possible with mutation.


Look at it closely - it's exactly the same as your OCaml example, except it uses ";" instead of "in", and indentation is adjusted accordingly.

Although in practice I found that it's common to not indent adjacent nested let..in blocks in OCaml, either, so you'd often see something like:

  let foo = "..." in
  let foo = parse(foo) in
  let foo = escaped(foo) in
  ...
And the rules are really simple - "let" always introduces a new binding.


You can't mutate variables in rust without declaring mut


But how do you know just by reading that code? It would not be obvious to me at all.


I think it's quite fine to assume the reader knows enough rust to know the difference between mutable and immutable variables.


let foo = bar; let foo = bat;

is shadowing

let foo = bar; foo = bat;

is a compilation-time error because foo isn't mutable.

let mut foo = bar; foo = bat;

is reassignment.

in working code, either foo is declared as mutable or it's not, and it's pretty obvious from the code what's happening.


I guess. I would just never write code like that.


You mean if you don't know the language?


I guess I just need to learn Rust. I wouldn't write a style of code where I have 3 lets after an other and using the same variable name.


I guess if you don't know rust you might not? But one of the first things you learn about rust is the `mut` keyword.


`let` rebinds. If there isn't a `let`, it's mutation.


It's really shadowing. The type can change and it isn't declared mutable.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: