Rust, Software Development

How To Declare And Use Variables In Rust

Variables in Rust are similar to variables in other high-level programming languages, and how we declare and use them is pretty straightforward. There are several types of variables, and their declaration may differ from each other.

Types Of Variables In Rust

A variable can be a global variable, a local variable, a function parameter, or a struct field. As such, its declaration may require initialization at the same time.

Global Variable

A variable is a global variable when we declare it outside of any code blocks with the static keyword. We need to specify the data type and value when we declare the variable.

Local Variable

If we declare it inside a function, it is typically a local variable, and the data type may be optional in Rust.

It can also be part of a for loop. In this case, we cannot specify its data type.

Function Parameter

If we declare a variable as part of a Rust function definition, it is a function parameter. We need to specify its data type.

Struct Field

A variable declared in a struct is a struct field. We only need to specify its data type. However, when we instantiate the struct Person, we need to specify appropriate values for the fields

Declare A Variable With a Valid Name

When naming a variable, we can use letters, digits, and the underscore character. However, it must start with either a letter or an underscore. Moreover, a variable name with lowercase letters differs from a variable with uppercase letters because Rust is case-sensitive.

There are some restrictions on variable names in Rust.

  • We must begin variable names with a letter or underscore (_) character.
  • We can only use letters, numbers, and the underscore character for variable names.
  • Variable names are case-sensitive, so my_variable and my_Variable are considered two different variables.

Consider the following examples of valid and invalid variable names in Rust:

  • my_variable (valid)
  • myVariable1 (valid)
  • _myVariable (valid)
  • my-variable (invalid – cannot contain hyphens)
  • let (invalid – cannot be a reserved keyword)
  • 1myVariable (invalid – must begin with a letter or underscore)

Declare And Assign Value In Rust

A variable in Rust can have an initial value when we declare it. But it depends on where we declare and how we use the variable. We can also assign it value after the declaration, depending on whether it is a mutable or immutable variable. By default, all variables are immutable in Rust.

For instance, these codes will fail compilation.

To declare a mutable variable in Rust, we use the mut keyword.

Using Variables

Although not immediately apparent when using Rust’s scalar primitive types, values have ownership and can change owners – from one variable to another. For example, these codes build and run successfully:

However, these will not compile:

Declaring and using variables is pretty straightforward until we start using custom data types. Then, we will have to deal with ownership and lifetimes.

This post is now part of the Rust Programming Language For Beginners Tutorial.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like