RUST > VARIABLES, CONSTANTS, AND DATA TYPES

Variables, Constants, and Data Types

In Rust, variables are declared using the let keyword. All variables are immutable by default, which means once a value is bound to a variable, it cannot be changed. If you want to make a variable mutable, the mut keyword is used. May - 25/2025

Comments, print and format!

a. Comments

Example:

    
    fn main () {

        // line;
              
        /*
            block;
        */
    }
        

Rust Playground: link!

b. Print

Example:

            
    fn main () {

        // No break line!
        print!("Hello, world!");
        
        //break line!
        println!("Hello, Maria!");
        println!("Hello, Carlos!");
    }
        

Rust Playground: link!

c. line break: \n

Example:

            
    fn main () {
  
        // line break: \n
        println!("What is your name?\nMy name is Rodolfo!");

        // : \n
        println!("What is your name?\\nMy name is Rodolfo!");
        
    }
        

Rust Playground: link!

d. format!

Example:

            
    fn main () {

        format!("Hello");                 // => "Hello"

        format!("Hello, {}!", "world");   // => "Hello, world!"

        format!("The number is {}", 1);   // => "The number is 1"

        format!("{:?}", (3, 4));          // => "(3, 4)"

        format!("{value}", value = 4);    // => "4"

        let people = "Rustaceans";
        format!("Hello {people}!");       // => "Hello Rustaceans!"

        format!("{} {}", 1, 2);           // => "1 2"

        format!("{:04}", 42);             // => "0042" with leading zeros

        format!("{:#?}", (100, 200));     // => "(
                                          //       100,
                                          //       200,
                                          //     )"
        
    }       
        

Rust Playground: link!

Post: