Implicit And Explicit
In all our previous examples we've set the
type of our variable explicitly by adding
a :
after the name along with the type
(e.g. i32
). Rust has the ability to
guess the type of some variables so
that's not always necessary. When we do
that it's called an "implicit" type
assignment and it looks like this:
let alfa = 7;
That turns our formula for defining a variable into this:
- The
let
keyword - The optional
mut
keyword - A name for the variable (e.g.
alfa
) - A
:
separator if we're explicitly setting the data type - An optional data type
- The
=
sign - The value to bind to it (e.g.
7
) - A
;
the ends the definition
In this example, both alfa
and bravo
have an i32
type. Alfa is defined
explicitly. Bravo is defined implicitly.
fn main() {
let alfa: i32 = 7;
let bravo = 9;
println!("Alfa {} - Bravo {}", alfa, bravo)
}