005 Desarrollo
005.1 Desarrollo

Impresión de Números Enteros

If you want to print My number: 140, you can type:

fn main() { print!("My number: 140"); }

or, using a placeholder and an additional argument:

fn main() { print!("My number: {}", "140"); }

or, removing the quotes around the second argument:

fn main() { print! ("My number: {}", 140); }

In the last statement, the second argument is not a literal string, but it is a literal integer number, or, for short, a literal integer.

The integers are another data type, with respect to string.

The print macro is also able to use integer numbers to replace the corresponding placeholder inside its first argument.

In fact, the compiler interprets the string 140 contained iin the source code as a number expressed in decimal format. It generates the equivalent number in binary format, and then it saves that binary number into the executable program.

At run time, the program takes that binary number; it transforms it into the string "140", using the decimal notation; then it replaces the placeholder with that string, therefore generating the string to print; and finally it sends the string to the terminal.

This procedure explains, for example, why if the following program is written:

fn main() { print!("My number: {}", 000140); }

the compiler generates exactly the same executable program generated before. Actually, when the source string 000140 is converted to the binary format, the leading zeros are ignored.

The argument types may be mixed too. This statement:

fn main() { print!("{}: {}", "My number", 140); }

will print the same line as before. Here the first placeholder corresponds to a literal string while the second one corresponds to a literal integer.