# Stuck on Stage #DE8 - Rust

**URL:** <https://forum.codecrafters.io/t/stuck-on-stage-de8-rust/15307>\
**Category:** Challenges\
**Tags:** challenge:interpreter\
**Created:** [September 17, 2025, 4:10pm UTC](https://forum.codecrafters.io/t/stuck-on-stage-de8-rust/15307 "2025-09-17T16:10:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurwi30](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/gurwi30/32/17304_2.png) [@Gurwi30](https://forum.codecrafters.io/u/Gurwi30)\
**Post date:** [September 17, 2025, 4:10pm UTC](https://forum.codecrafters.io/t/stuck-on-stage-de8-rust/15307/1 "2025-09-17T16:10:17Z")

</div>

I’m stuck on Stage #DE8.  
[GitHub - Gurwi30/interpreter-rust](https://github.com/Gurwi30/interpreter-rust)

I have a issue running this lox code, for some reason it says that the hello variable is not defined, but i retry by using a while loop it works fine.

`for (var hello = 0; hello < 3; hello = hello + 1) { print hello; }`

I’ve tried almost everything i could think of, .

Here are my logs:

```auto
Logs from your program will appear here!
(< var IDENTIFIER hello null 3.0) { { var IDENTIFIER hello null } IDENTIFIER hello null = (+ var IDENTIFIER hello null 1.0) }
[Print { expr: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 2 } } }]
[Block { statements: [Print { expr: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 2 } } }] }, Expression { expr: Assign { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 }, value: Binary { left: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 } }, operator: Token { token_type: Plus, lexeme: "+", literal: None, line: 1 }, right: Literal { literal: Float(1.0) } } } }]
[Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 }, initializer: Literal { literal: Float(0.0) } }, While { condition: Binary { left: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 } }, operator: Token { token_type: Less, lexeme: "<", literal: None, line: 1 }, right: Literal { literal: Float(3.0) } }, body: Block { statements: [Block { statements: [Print { expr: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 2 } } }] }, Expression { expr: Assign { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 }, value: Binary { left: Variable { name: Token { token_type: Identifier, lexeme: "hello", literal: None, line: 1 } }, operator: Token { token_type: Plus, lexeme: "+", literal: None, line: 1 }, right: Literal { literal: Float(1.0) } } } }] } }]
Env
Current environment 0x1b179931450
Dept 1, Lexeme hello
Environment at depth 0: {"hello": Float(0)}
        Pointer { addr: 0x1b179937150, metadata: 5 } 0
  Environment at depth 1: {"clock": Callable(<fn>), "print": Callable(<fn>)}
        Pointer { addr: 0x1b179936f60, metadata: 5 } <fn builtin clock>
        Pointer { addr: 0x1b179937080, metadata: 5 } <fn builtin print>
Ancestor
  Environment at depth 1: {"clock": Callable(<fn>), "print": Callable(<fn>)}
        Pointer { addr: 0x1b179936f60, metadata: 5 } <fn builtin clock>
        Pointer { addr: 0x1b179937080, metadata: 5 } <fn builtin print>
Search Pointer { addr: 0x1b179937070, metadata: 5 }
[line 1] Error: Undefined variable 'hello'

```

And here’s a snippet of my code:

```python
fn look_up_var(&self, name: &Token, expr: &Expr) -> Result<Value, ExecError> {
        match self.locals.get(expr) {
            Some(distance) => match Environment::get_at(self.environment.clone(), *distance, name.lexeme.as_str()) {
                Some(val) => Ok(val),
                None => {
                    println!("Env");
                    println!("Current environment {:p}", Rc::as_ptr(&self.environment));
                    println!("Dept {}, Lexeme {}", distance, name.lexeme);
                    self.environment.borrow().debug_print(0);
                    
                    println!("Ancestor");
                    
                    Environment::ancestor(Rc::clone(&self.environment), *distance).borrow().debug_print(*distance);
                    
                    println!("Search {:p}", name.lexeme.as_str());
                    
                    if let Some(v) = Environment::get_at(self.environment.clone(), *distance, "hello") {
                        println!("Found {}", v);
                    } 
                    
                    Err(ExecError::Runtime(
                        RuntimeError::new(name.clone(), format!("Undefined variable '{}'", name.lexeme)), )
                    )
                },
            },
            None => self.globals.borrow().get(name)
        }
    }

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [September 24, 2025, 3:14am UTC](https://forum.codecrafters.io/t/stuck-on-stage-de8-rust/15307/4 "2025-09-24T03:14:24Z")

</div>

> but i retry by using a while loop it works fine.

Hey @Gurwi30, would you mind sharing a bit more about the while loop you used?

---

<div class="post-metadata">

**Author:** ![Gurwi30](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/gurwi30/32/17304_2.png) [@Gurwi30](https://forum.codecrafters.io/u/Gurwi30)\
**Post date:** [September 25, 2025, 4:02pm UTC](https://forum.codecrafters.io/t/stuck-on-stage-de8-rust/15307/6 "2025-09-25T16:02:08Z")

</div>

You can find the source code on github, the loop i used is just a basic for loop but resolver is not resolving it. Because currently the for loop just gets converted in a while loop so it gets converted into this

```auto
{
    var hello = 0;
    while (hello < 3) {
        print hello;
        hello = hello + 1;
    }
}

```

and if i write the while loop like that it works, but for some reason the resolver is unable to resolve the hello var when it’s written as a for lopp

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 10, 2025, 7:34am UTC](https://forum.codecrafters.io/t/stuck-on-stage-de8-rust/15307/8 "2025-11-10T07:34:32Z")

</div>

@Gurwi30 I’m no longer able to access your GitHub repo. Let me know if you still need help with this!

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 16, 2025, 1:40pm UTC](https://forum.codecrafters.io/t/stuck-on-stage-de8-rust/15307/10 "2025-11-16T13:40:43Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [November 21, 2025, 1:40pm UTC](https://forum.codecrafters.io/t/stuck-on-stage-de8-rust/15307/11 "2025-11-21T13:40:58Z")

</div>

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