Which compiles fine on Rust Playground. However, for whatever reason, when compiling on Windows 10, I get the error "thread 'main' has overflowed its stack".
Note: The stack overflow happens when running the program, not when compiling it.
This happens because arr takes up about 1.4 MB of stack space. Limits on stack space are different on different machines and platforms, but generally it's a good idea to store large values (hundreds of KB or larger) on the heap. You can do this by changing arr from an array to a Vec:
Note that on Windows you can set your applications stack size using a linker flag. Oddly Rust sticks with the fallback value of 1 MiB for the main thread but (by default) sets it to 2 MiB when creating threads.
But yes, the heap is always preferable for large values.
Sure. But it depends on the linker. For the msvc toolchain it's something like /STACK:8000000 which will set the stack to 8 megabytes. For the gnu toolchain it's --stack 8000000 (took me awhile to find where that's documented).
Setting this in Rust is a bit awkward. You need to create a folder in the root of your project directory called ".cargo" and then add a file to it called "config.toml". In that file you can set custom linker options:
Since stack space is determined by the compiler, atleast it can warn when more stack space being used in the function. I don't understand since the size atleast can be already determined here at compile time. In other compilers(like gcc) also dont give warning for allocating this much space.
This is platform dependent. On some OSes it's defined by the linker but on other it's set only by the OS. So Rust can't always know how much stack space you have. EDIT: Clippy does have a warning for large stack arrays.
Btw, if you do want to control the stack size in a more cross-platform way then it's better to build a new thread.using the stack_size option.