Background
Let us do a quick Hello World Program in Rust.
Steps
- Create Project
- Code
- Build Project
- 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
- Files
- Package Handling ( manifest file )
- cargo.toml
- Add required modules
- cargo.toml
- Source Code
- src\main.rs
- Add a bit more code to the main file ( main.rs )
- src\main.rs
- Package Handling ( manifest file )
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.
- Project
- cargo new
- Solid Folder Structure
- Stub Files Created
- cargo.toml
- src\main.rs
- cargo new
- Convention
- Quite Heady about names
- Project Names
- File Names
- Variable Names
- Quite Heady about names