
Rust Essentials
By :

Often, an application needs a few values that are in fact constants, meaning that they do not change in the course of the program. In our game, for example, the game name Monster Attack
could be a constant, as could the maximum health amount, which is the number 100
. We must be able to use them in the main()
function or any other function in our program, so they are placed at the top of the code file. They live in the global scope of the program. Such constants are declared with the keyword static
, as follows:
// see Chapter 2/code/constants1.rs static MAX_HEALTH: i32 = 100; static GAME_NAME: &str = "Monster Attack"; fn main() { }
Names of constants must be in uppercase, underscores can be used to separate word parts. Their type must also be indicated; the variable MAX_HEALTH
is a 32-bit integer (i32
) and the variable GAME_NAME
is a string (str
) type. As we will discuss further, the declaration of types for variables is done in exactly the same way, although it...
Change the font size
Change margin width
Change background colour