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

help-circle
  • I was a bit apprehensive because rust has like a gazillion different function types but here it seems to work like just any other language with a HM type system.

    The fn(T)->R syntax works for functions without associated data, it discards details of the implementation and works like function pointers in C. This allows them to be copy and 'static.

    The other function types can have data with them and have more type information at compile time which allows them to be inlined.
    These functions each have their own unwritable type that implements the function traits (Fn(T)->R, FnMut(T)->R and FnOnce(T)->R) depending on their enclosed data.

    I hope I remembered everything right from this video by Jon Gjengset.






  • 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