How do I avoid stack overflow at compile time?

Viewed 118

General performance advice for Rust is to try to avoid placing things on the heap if possible.

An issue I am having is that I do not know where/when the size limit of a function stack will be reached, until my program panics unpredictably at runtime.

Two examples are:

  • Parsing deeply nested structs from JSON using Serde.
  • Creating many futures inside a function.

Questions:

  • Can I avoid this by detecting it at compile time?

  • How can I know what the limit of the stack is whilst I am writing code? Do others just know the exact size of their variables?

  • Why do people advise to try to avoid the heap?

0 Answers
Related