Rust:- Hello World ( Customized )

Background

Let us do a quick Hello World Program in Rust.

Steps

  1. Create Project
  2. Code
  3. Build Project
  4. Run Application

Tasks

Create Project

Command

Syntax


cargo new <applicationName> --<applicationType>

Sample


cargo new helloWorld --bin

Output

Output – Image

Output – Text


>cargo new helloWorld --bin
     Created binary (application) `helloWorld` package

>

Code

Outline

  1. Files
    • Package Handling ( manifest file )
      • cargo.toml
        • Add required modules
    • Source Code
      • src\main.rs
        • Add a bit more code to the main file ( main.rs )

 

Tasks

Files

Package Handling ( Manifest File )
cargo.toml

[package]
name = "helloWorld"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

# Added by dadeniji on 2022-09-14 09:02 AM
# This line identifies the whoami crate as one of the dependencies
# https://crates.io/crates/whoami/0.9.0
whoami = "0.9.0"

Source Code
src\main.rs

fn main() {
    
    //println!("Hello, world!");
    
    say_hello();
    
}

/*
    WhoAmI
    https://crates.io/crates/whoami/0.9.0
*/
fn say_hello(){

    /*
        Declare Variables
    */
    let user_realname:String;
    let user_hostname:String;
    let user_username:String;

    /*
        Set Variable Values
    */
    user_realname = whoami::realname();
    
    user_hostname = whoami::hostname();

    user_username = whoami::username();
        
    // Print text to the console
    println!("Hello {0}", user_realname);       
    
    println!(
                  "Your username on this system ({0}) is {1}"
                , user_hostname
                , user_username
            );    
 
    
}

Build Project

Command

Syntax


cargo build

Sample


>cargo build
warning: crate `helloWorld` should have a snake case name
  |
  = note: `#[warn(non_snake_case)]` on by default
  = help: convert the identifier to snake case: `hello_world`

warning: `helloWorld` (bin "helloWorld") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s

>


Output

Output – Image

Output – Text


>cargo build
warning: crate `helloWorld` should have a snake case name
  |
  = note: `#[warn(non_snake_case)]` on by default
  = help: convert the identifier to snake case: `hello_world`

warning: `helloWorld` (bin "helloWorld") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s

>

Run Project

Command

Syntax


cargo run

Sample


cargo run

Output

Output – Image

Output – Text


>cargo run
warning: crate `helloWorld` should have a snake case name
  |
  = note: `#[warn(non_snake_case)]` on by default
  = help: convert the identifier to snake case: `hello_world`

warning: `helloWorld` (bin "helloWorld") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target\debug\helloWorld.exe`
Hello Adeniji, Daniel
Your username on this system (devmachine01) is dadeniji

>

Summary

Like Dixie Chicks took the long way home on this one.

Points Made.

  1. Project
    • cargo new
      • Solid Folder Structure
      • Stub Files Created
        • cargo.toml
        • src\main.rs
  2. Convention
    • Quite Heady about names
      • Project Names
      • File Names
      • Variable Names

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s