• 0 Posts
  • 5 Comments
Joined 1 year ago
cake
Cake day: July 1st, 2023

help-circle


  • I feel like it’s like pointers.

    “Variable” refers to the label, i.e. a box that can contain anything (like *ptr is a pointer to [something we dont know anything about])

    Immutable describes the contents, i.e. the stuff in the box cant change. (like int* ptr describes that the pointer points to an int)

    Rust makes it very obvious that there’s a difference between constants and immutable variables, mainly because constants must be compile time constants.

    What do you call it when a variable cant change after its definition, but isnt guaranteed to be the same on each function call? (E.g. x is an array that’s passed in, and we’re just checking if element y exists)

    It’s not a constant, the contents of that label are “changing”, but the label’s contents cant be modified inside the scope of that function. So it’s a variable, but immutable.



  • For downsides, i’d like to add that the lack of function overloading and default parameters can be really obnoxious and lead to [stupid ugly garbage].

    A funny one i found in the standard library is in time::Duration. Duration::as_nanos() returns a u128, Duration::from_nanos() only accepts a u64. That means you need to explicitly downcast and possibly lose data to make a Duration after any transformations you did.

    They cant change from_nanos() to accept u128 instead because that’s breaking since type casting upwards has to be explicit too (for some reason). The only solution then is to make a from_nanos_u128() which is both ugly, and leaves the 64 bit variant hanging there like a vestigial limb.