VARIABLES AND CONSTANTS

  

VARIABLES AND CONSTANTS

VARIABLES

  • Variables in rust by default are immutable.
  • let x = 42;
  • You can make it mutable by adding mut like below example.

  • let mut x = 42;
    x = 143;
  • Once a data type is assinged it can't be changed, unless shadowing is used.

CONSTANTS

  • Like immutable variables, constants are not allowed to change.
  • They cannot be initialized as empty.
  • Although variables can be assgined constant values, the vice versa is not possible.
  • Wriiten using keyword const
  • const DEFAULT_VALUE: &str = "Apple";


SHADOWING

  • The ability to declare a new varible name multiple names.
  • It allows to even change the data type of the variable like below given example.
  • let age: f64 = 32.35;
    let age: &str = "grapes";

Comments