Home > Projects > Hello World




Rust


Rust is a general-purpose, sponsored by Mozilla Research. It is designed to be safe, concurrent, and practical.

It grew from a project by Mozilla employee Graydon Hoare, and Mozilla began sponsoring the project in 2009. Although its development is sponsored by Mozilla, it is an open community project.


Rust Advantages


Rust has many strengths and some are distinctive.


Rust Disadvantages



For more information about Rust visit these links:




Simple Hello World


Version rustc 1.8.0

// This is the main function

fn main() {

  println!("Hello, World!");   // Print text to the console
}

Strings are an important concept for a programmer to master. In Rust string handling is somewhat different from other languages

Rust has two main types of strings: &str and String.

fn main() {

//String slice example:  

let string = "Hello, World!";      //string: &str
println!("{}", string);


//String example:    
//Strings are growable:

let mut s = "Hello".to_string();   //mut s: String
s.push_str(", World!");
println!("{}", s);

}

Both examples above print the following:


Hello, World!



Hello World Ascii Variation


Create a file called rusthello.rs


Then define two functions, with a for loop statement that repeatedly prints the "#" or "space" characters, until the specified number of repetitions evaluate to false. The parameter specifying the number of times to repeat the "#" or "space" is named a.


Two other functions are defined for when a line return is needed. These are named draw_last_pound() and draw_last_space()

Note that in the other language examples they were named drawPound for example. Rust requires the underscore and lowercase and otherwise generates a warning on compile that the function names should have a "snake case name".

fn main() {
    draw_pound(1); draw_space(3); draw_pound(1); draw_space(1); draw_pound(5); draw_space(1); draw_pound(1); draw_space(5); draw_pound(1); draw_space(6); draw_pound(3); draw_space(5); draw_pound(1); draw_space(7);
    draw_pound(1); draw_space(2); draw_pound(3); draw_space(2); draw_pound(4); draw_space(2); draw_pound(1); draw_space(5); draw_pound(4); draw_space(3); draw_last_pound(1); draw_pound(1); draw_space(3); draw_pound(1);
    draw_space(1); draw_pound(1); draw_space(5); draw_pound(1); draw_space(5); draw_pound(1); draw_space(5); draw_pound(1); draw_space(3); draw_pound(1); draw_space(4); draw_pound(1); draw_space(3); draw_pound(1);
    draw_space(3); draw_pound(1); draw_space(1); draw_pound(1); draw_space(3); draw_pound(1); draw_space(1); draw_pound(1); draw_space(3); draw_pound(1); draw_space(1); draw_pound(1); draw_space(5); draw_pound(1);
    draw_space(3); draw_pound(1); draw_space(2); draw_last_pound(1); draw_pound(5); draw_space(1); draw_pound(5); draw_space(1); draw_pound(1); draw_space(5); draw_pound(1); draw_space(5); draw_pound(1); draw_space(3);
    draw_pound(1); draw_space(4); draw_pound(1); draw_space(3); draw_pound(1); draw_space(3); draw_pound(1); draw_space(1); draw_pound(1); draw_space(3); draw_pound(1); draw_space(1); draw_pound(4); draw_space(2);
    draw_pound(1); draw_space(5); draw_pound(1); draw_space(3); draw_pound(1); draw_space(2); draw_last_pound(1); draw_pound(1); draw_space(3); draw_pound(1); draw_space(1); draw_pound(1); draw_space(5);
    draw_pound(1); draw_space(5); draw_pound(1); draw_space(5); draw_pound(1); draw_space(3); draw_pound(1); draw_space(5); draw_pound(1); draw_space(2); draw_pound(1); draw_space(2); draw_pound(1); draw_space(2);
    draw_pound(1); draw_space(3); draw_pound(1); draw_space(1); draw_pound(1); draw_space(2); draw_pound(1); draw_space(2); draw_pound(1); draw_space(5); draw_pound(1); draw_space(3); draw_pound(1); draw_space(2);
    draw_last_space(1); draw_pound(1); draw_space(3); draw_pound(1); draw_space(1); draw_pound(5); draw_space(1); draw_pound(5); draw_space(1); draw_pound(5); draw_space(2); draw_pound(3); draw_space(2);
    draw_pound(1); draw_space(4); draw_pound(1); draw_space(2); draw_pound(1); draw_space(5); draw_pound(3); draw_space(2); draw_pound(1); draw_space(3); draw_pound(1); draw_space(1); draw_pound(5); draw_space(1);
    draw_pound(4); draw_space(3); draw_last_pound(1); draw_space(29); draw_pound(1); draw_space(38); draw_last_space(1); 
}
// n: int no longer works see all the different i8 u8 etc
fn draw_pound(n: i8) {   
    let mut i = 1;
    while i <= n {
        print!("#");
        i += 1;
    }
}

fn draw_space(n: i8) {
    let mut i = 1;
    while i <= n {
        print!(" ");
        i += 1;
    }
}

fn draw_last_pound(n: i8) {
    let mut i = 1;
    while i <= n {
        println!("#");
        i += 1;
    }    
}

fn draw_last_space(n: i8) {
    let mut i = 1;
    while i <= n {
        println!(" ");
        i += 1;
    }
}

Follow the steps to create the Rust executable:



...\Rust stable GNU 1.8\project\rusthello>rustc rusthello.rs

...\Rust stable GNU 1.8\project\rusthello>rusthello.exe


#   # ##### #     #      ###     #       #  ###  ####  #     ####   #

#   # #     #     #     #   #    #   #   # #   # #   # #     #   #  #

##### ##### #     #     #   #    #   #   # #   # ####  #     #   #  #

#   # #     #     #     #   #     #  #  #  #   # #  #  #     #   #   

#   # ##### ##### #####  ###  #    #  #     ###  #   # ##### ####   #

                             #                                       



Rust (Two-dimensional Array)


There are better and simpler ways to print Ascii art than this two-dimensional array example. In the past I worked with junior developers who didn't understand arrays. Basically here is the type of example I showed them to illustrate the concept.


Periods were used as placemarkers for the spaces to make it visually easier.



Create a file called rusthelloarray.rs

fn main() {

const ROWS: i32 = 5;
const COLS: i32 = 11;

let mut hello = [[" "; COLS as usize]; ROWS as usize];

For the populating the hello array, refer to the illustration above to add elements to it:

//row 1
    hello[0][0] = " _    ";
    hello[0][1] = "     ";
    hello[0][2] = " _ ";
    hello[0][3] = " _ ";
    hello[0][4] = "      ";
    hello[0][5] = "         ";
    hello[0][6] = "     ";
    hello[0][7] = "     ";
    hello[0][8] = " _ ";
    hello[0][9] = "    _ ";
    hello[0][10] = " _ ";
    
  //row 2
    hello[1][0] = "| |__ ";
    hello[1][1] = " ___ ";
    hello[1][2] = "| |";
    hello[1][3] = "| |";
    hello[1][4] = " ___  ";
    hello[1][5] = " ___  ___";
    hello[1][6] = " ___ ";
    hello[1][7] = " ___ ";
    hello[1][8] = "| |";
    hello[1][9] = " __| |";
    hello[1][10] = "| |";
    
  

Note the use of double backslashes on some of the strings where escape characters needed to be used as actual characters in the Ascii art.

(e.g. val str = "\\" instead of "\" )

  //row 3
  hello[2][0] = "|    \\";
  hello[2][1] = "/ __\\";
  hello[2][2] = "| |";
  hello[2][3] = "| |";
  hello[2][4] = "/ _ \\ ";
  hello[2][5] = " \\  ||  /";
  hello[2][6] = "/ _ \\";
  hello[2][7] = "|  _\\";
  hello[2][8] = "| |";
  hello[2][9] = "/ _' |";
  hello[2][10] = "| |";
   
//row 4   
  hello[3][0] = "|_||_|";
  hello[3][1] = "\\___|";
  hello[3][2] = "|_|";
  hello[3][3] = "|_|";
  hello[3][4] = "\\___/ ";
  hello[3][5] = "_ \\_/\\_/ ";
  hello[3][6] = "\\___/";
  hello[3][7] = "|_|  ";
  hello[3][8] = "|_|";
  hello[3][9] = "\\____|";
  hello[3][10] = "|_|";
 
//row 5
  hello[4][0] = "      ";
  hello[4][1] = "     ";
  hello[4][2] = "   ";
  hello[4][3] = "   ";
  hello[4][4] = "      |/";
  hello[4][5] = "       ";
  hello[4][6] = "     ";
  hello[4][7] = "     ";
  hello[4][8] = "   ";
  hello[4][9] = "      ";
  hello[4][10] = "(_)";   
 
 

Next, iterate over the array with for loops:

   
  for i in 0..ROWS as usize {
    for j in 0..COLS as usize {
    print!("{}", hello[i][j]);
  }
  println!(""); 
  }   
}

Follow the steps to create the Rust executable:



...\Rust stable GNU 1.8\project\rusthelloarray>rustc rusthelloarray.rs

...\Rust stable GNU 1.8\project\rusthelloarray>rusthelloarray.exe


 _          _  _                           _     _  _ 

| |__  ___ | || | ___   ___  ___ ___  ___ | | __| || |

|    \/ __\| || |/ _ \  \  ||  // _ \|  _\| |/ _' || |

|_||_|\___||_||_|\___/ _ \_/\_/ \___/|_|  |_|\____||_|

                      |/                           (_)



Rust -- One Last Array Example


Here is an array example using integers, which provides a comparison/contrast to the previous example using strings.

Since any documentation and examples were difficult to find on Rust arrays, I have decided to include the following as reference.

Some items to note between single dimensional arrays, two-dimensional arrays, integers, and strings are:


Single Dimensional Array / Integer:


Two Dimensional Array / String:


fn main() {
let mut array: [i32; 6] = [0; 6];
array[1] = 1;
array[2] = 2;
array[3] = 3;
array[4] = 4;
array[5] = 5;

for x in &array {
    print!("{} ", x);
}
}

This prints the following:


0 1 2 3 4 5