Storing values with Variables in Rust

braindead
1 min readAug 13, 2022

--

let mut guess = String::new();

We use the let statement to create the variable.

Here’s another example:

let cups = 6;

This line creates a variable named cups and bind it’s to the value 6. In Rust, variable are immutable by default.

Immutable object is an object whose state cannot be modified after it is created.

This is in contrast to a mutable object, which can be modified after it is created.

To make variable mutable we add mut before the variable name. For example:

let mut bananas = 7;

The above line will return a mutable variable named bananas which is binded to the value 7. But in this case the value of our variable bananas can be mutated to some other value because our variable bananas is a mutable variable.

The equal sign (=) tells Rust we want to bind something to the variable now.

let mut guess = String::new();

On the right of the equals sign is the value that guess is bound to, which is the result of calling String::new, a function that returns a new instance of a String. String is a string type provided by the standard library that is a growable.

And forget not, S in String must be in capital while writing syntax in rust code.

--

--

braindead
braindead

Written by braindead

A passionate programmer, I am eager to challenge myself to do things I’ve never accomplished before and I strive to learn and improve on my skills every day

Responses (1)