• 0 Posts
  • 36 Comments
Joined 1 year ago
cake
Cake day: August 4th, 2023

help-circle






  • This behavior is simple, it just wasn’t explained to you at the correct level.

    All blocks (code between { and }) evaluate to some value, they’re all treated as expressions. A function “without a return type” will always return () because that’s what a block that doesn’t have any “output” “returns”.

    This understanding is fundamental to Rust and, for example, is involved in understanding when we have to and don’t have to use return in a function. Quite usefully, it also allows us to organize our code by creating a new scope, i.e.

    let foo: u32 = {
        // Hate dropping mutex guards explicitly? This is one way you can avoid that (although it's arguably a code smell that I discourage and should not have even mentioned)
    
        // This block evaluates to 100
        100
    };
    
    let foo: () = {
        println!("this block evaluates to the unit type");
    }
    

    The way Rust interprets the issue with your function is:

    1. This function returns a unit type () because it doesn’t have an explicit return value
    2. The block that defines this function is evaluating to a value of type i32 which is not ()
    3. errors

    The reason the error is perhaps more unclear than it could be is because they assume you have read the Rust book or understand how blocks are expressions themselves.

    It would be valuable to look at https://doc.rust-lang.org/book/ch03-03-how-functions-work.html for more details and to gain a more thorough understanding of the topic at hand. I left out the precise details on “which expression from the block is the one that it evaluates to” other than the obvious case of “the very last expression”.



  • f32 is a type, you need to actually provide a value to the shorthand array initialization syntax, the array is filled with that value. There is no such thing as “uninitialized” or implicit zeroing here.

    let foo: [f32; 5] = [0.0; 5];

    You seem to have indicated dismay over the default f64 type.

    To type a integer or float literal, suffix it with its built-in type. For example:

    0.0f32

    1024usize

    Does this look ugly? Yes. Good news! It’s hardly relevant in an actual project because Rust will easily default to whichever float type you are using throughout the project as your floating point numbers with resolved types will resolve the types of the implicitly typed floating point numbers.

    You can also use the same syntax with the vec![] macro :)

    let bar = vec![0.0f32; 5];









  • I wont use all caps but they are indeed all over the netherlands. This discussion is hella lacking context. Yes those racks are very common and normal, and no amount of what you think changes that. This is a dutch ad which is based on what NL actually looks like.

    Acting like the 80s were a new time for bikes… by then the netherlands was not occupied and the dutch were indeed allowed to own bikes again :)