Why stack overflow here?

Is there a stack overflow because this function creates an infinite loop?

fn fibonacci(n: &str){
    match n {
        "test" => fibonacci(&n[..2]),
        _ => fibonacci(&n[..2]),

fn main()

thread 'main' has overflowed its stack

Because these code will cause an infinitely recursive call.

I suppose you want the fibonacci function to return when n is not "test", but the _ arm in match will also lead to another fibonacci call.

So whatever n is, match will end with a new fibonacci call. The first n will be "test", then will be "te" forever.

the "_" arm means default, as if no other arms matched, go with the default one.

and because it will run forever there will be a stack overflow is it correct?

It isn't because it runs forever, it's because each function calls reserves stack space (and gives it back when it returns), calling a very large number of nested functions (never returning) exhausts the stack.


Yep, every function has a stack, with a fixed initial size. When functions were called inifinately, the remaining memory space would be exhausted very soon. Then a stack overflow occurs.

i.e. here functions are called forever and consume heap space every time they are called i hope i got it right

Functions consume stack space (not heap space) for their variables for as long as they are running, in particular this function is storing n on the stack. Since it's a recursive function, there are more and more copies of n being created at the same time.

However, if you compile this in release mode rather than in debug mode, tail call optimization kicks in and this turns into an infinite loop that does not consume the stack.


Mostly, with a minor nitpick:
Not heap space, stack space located in a different segment.

In modern operating systems, memory is usually stored in segments, such as Stack Segment, Data Segment, BSS, Data Segment, Code Segment... Each has its own space with a different length.

So the infinite loop function actually runs out of the stack memory segment, then a stack overflow was given. But if it is not the stack space that is depleted, the error will be varied.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.