samyyc.dev
LOC::Macau, China

..UTC+8

samyyc.dev

-- BPM

Mon May 29 2023

Rust Learning Notes 1: Variables

#83

/

samyyc

/

559 words

/

Updated 1285 days ago

/

[中文]

Rust Learning Notes 1: Variables

I skimmed through Rust before and roughly got the basic concepts and syntax, but I never had anywhere to actually use it. I'm afraid I'll forget it in a few days, so I'm writing this post as a review.

The structure of this series will differ from a normal tutorial; it follows my own personal ordering more.

Declaring Variables

You use the let keyword to declare a variable.

There's a conceptual point here: in Rust, declaring a variable should be seen as binding a value to this variable. This has to do with Rust's ownership mechanism.

A Rust variable declaration looks roughly like this:

let a = 32;

That's the most ordinary variable declaration in Rust.

Basic Variable Data Types

Generally, Rust will infer the type of a variable automatically (type inference). Of course, you can also specify its type explicitly, like this:

// i32 is a 32-bit signed integer
let a: i32 = 32;

Rust roughly has the following data types:

  • u8 / i8
  • u16 / i16
  • u32 / i32
  • u64 / i64
  • u128 / i128
  • usize / isize (depends on the operating system)
  • f32 / f64 (32-bit float and 64-bit float)
  • bool
  • char (four bytes, Unicode)

Literals

Rust supports some special formats for declaring literals:

  • 98_222 represents the number 98222 (decimal)
  • 0xff hexadecimal
  • 0o77 octal
  • 0b1111_0000 binary
  • b'A' is u8 by default and can be converted to char

Mutability

Rust variables have a property: mutable and immutable. Mutable variables need to be explicitly declared with the mut keyword.

As the name suggests, a mutable variable's value can be changed. That is, if we don't declare a variable with the mut keyword, it can be compared to a constant (but there are some differences, which I'll mention later).

Code:

let mut a = 32;
// can be modified; without the mut keyword this would error
a = 64;

Shadowing

Rust has the concept of shadowing. A variable can be shadowed:

// declare any variable; the mut keyword is not needed
let a = 32;
// now a is shadowed by another variable
// shadowing means rebinding this a to another value; it's fundamentally different from mut
// mut modifies the original value
// another difference is that shadowing can change the variable's type, while mut cannot
let a = 64;

Shadowing can change the variable's type because it's essentially binding the variable to another value. This feature is very useful, for example when we're dealing with some parameters:

// assume data is the data returned by some network request here
// with three properties: code, data, and msg
let data = XXX;
// we want to get its data property directly
// shadow it directly
let data = data.data;

Constants

Rust uses the const keyword to declare constants. The syntax is as follows:

const A: i32 = 1;

Constants have several characteristics:

  • Cannot be declared as mut (pretty easy to understand)
  • Cannot be shadowed
  • Can be optimized by the compiler
  • The type must be specified when declaring; type inference cannot be used

Static Variables

Rust uses the static keyword to declare static variables. The syntax is as follows:

static A: i32 = 1;

The main characteristics of static variables are:

  • Ownership cannot be transferred
  • The lifetime is 'static

Static variables can be declared mutable, but to modify one we need to use an unsafe code block:

static mut A: i32 = 1;
unsafe {
  A = 5;
}

This one thing drags in three concepts (ownership, lifetimes, unsafe), so let's save it for later..