When learning Rust, understanding the difference between statically and dynamically sized types seems critical. There are some good discussions out th

Sizing Up Types in Rust

submited by
Style Pass
2021-07-16 19:00:12

When learning Rust, understanding the difference between statically and dynamically sized types seems critical. There are some good discussions out there already (e.g. here and here). Whilst these explain the mechanics, they didn’t tell me why its done like this in Rust. The articles made sense, but I was still confused! Eventually I had my “eureka” moment, so I figured I should share that.

I’m a C/C++/Java programmer (amongst other things) and the basic idea of a statically- versus dynamically-sized type seemed pretty straightforward: A statically-sized type is one whose size is known at compile time; and, a dynamically sized type is everything else. Easy as! For example, an int is statically sized in Java (i.e. because its a 32-bit two’s complement integer):

On the other hand I figured an array int[] in Java is dynamically sized (i.e. because we cannot determine how many elements it contains at compile time):

Leave a Comment