• 0 Posts
  • 16 Comments
Joined 1 year ago
cake
Cake day: July 24th, 2023

help-circle




  • You can’t random-access an iterator and use it again later.

    If your specific use case really needs random access to a list while lazy computing the elements just wrap them in Lazy and put them in a vector.

    Can Rust compute the value of calling a function an infinite number of times?

    The return type of an infinitely recursive function / infinite loops is ⊥, a type that by definition has no values. (Known in rust as !)






  • Such a case would be the single function having a side effect*. This allows the caller to chose when to execute the side effect or to drop the result without executing it.

    In my opinion that is fine with fn_once but not into because of the implicit contract of these traits.

    * = I’m counting expensive computation, but not allocation and memcopy as a side effect in this comment.





  • Recursion is often unintuitive for beginners, to understand this code we should simply it a bit

    
    int main(void)
    {
        int height = get_int("Height: ");
    
        draw(height);
    }
    
    void draw(int n)
    {
        if (n <= 0)
        {
            return;
        }
    
        draw(n - 1);
        printf("%d",  n);
        
        printf("\n");
    }
    

    Inputting 3 should now give us a output like

    1
    2
    3 
    

    Try to understand that case first and than muddle it up with the loop.

    If you examine it in a debugger look at the Stack trace. If you use gdb its bt in a visual debugger it’s probably the list of function names next to the variables