In the last article, we learned how to print custom data in Rust using the Debug and Display traits. Today, we're diving into the fundamentals: variables! Variables are the containers that store your data.
In Rust, we use the let keyword to declare a variable. Here's the basic structure:
let my_variable: data_type = value;
let: Tells Rust we're creating a variable.
my_variable: The name you choose for your variable.
data_type: The kind of data the variable will hold (more on this soon!).
value: The initial data you assign to the variable.
One key difference between strongly typed languages like C and loosely typed languages like Python lies in how you declare variables. In strongly typed languages like C, you must explicitly define the data type of each variable. Loosely typed languages, however, can infer the data type based on how the variable is used. Rust, while being strongly typed and compiled, adopts a slightly different approach. Let's try this example
In the above example, we declare a variable a of type unsigned int 32 (u32) and assign it a value of 1000. As expected, when we run the code, the value of 'a' is printed.
Don't Miss an Update!
Helping you navigate the Embedded Systems career with ease! Technical posts, newsletters, special offers, and more.