Just a little silly Rust program that creates a recursive struct and triggers a stack overflow.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Pixaurora 6da61519d2
Initial commit
Signed-off-by: Rina <rina@pixaurora.net>
2026-07-13 22:15:40 -04:00
src Initial commit 2026-07-13 22:15:40 -04:00
.gitignore Initial commit 2026-07-13 22:15:40 -04:00
Cargo.lock Initial commit 2026-07-13 22:15:40 -04:00
Cargo.toml Initial commit 2026-07-13 22:15:40 -04:00
README.md Initial commit 2026-07-13 22:15:40 -04:00

Ouroboros

The snake eating its tail. Funny little concept, so I made it in Rust using unsafe.

Relevant code:

let mut ouroboros = Snake {
    tail: SnakeTail::Normal,
};

unsafe {
    let ouroboros_ptr: *mut Snake = &mut ouroboros;

    ouroboros_ptr.write(Snake {
        tail: SnakeTail::Bitten(&ouroboros),
    });
};

println!(
    "The tail of the legendary creature, Ouroboros: {}",
    ouroboros.tail
);

// Stack over-flow happens here

println!("All done!");

The main function is pretty simple overall, we make a Snake, and then set its Tail to be bitten by a reference to itself.

Usually, Rust wouldn't let you do this, since you cannot use a reference to a value while mutating that value.

I get around this by making a pointer instead, and writing to that in the unsafe block.

The output:

The tail of the legendary creature, Ouroboros: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... Their tail: Wait, someone is biting it... 

(that keeps going for a while)

thread 'main' (140712) has overflowed its stack
fatal runtime error: stack overflow, aborting

Quite funny, we love recursion

But... Why?

Because why not? I just made this because I thought it would be kind of funny to break Rust's borrowing rules a little and cause some chaos.