Rust: A memory safe Systems Programming Language
rustup: An installer for the systems programming language Rust
cargo: Rust’s package manager and build system
Rust Installation
1. Installing rustup:
$ curl https://sh.rustup.rs -sSf | sh
– info: latest update on 2019-08-15, rust version 1.37.0
– info: downloading component ‘rustc’
– Rust is installed now. Great!
– The installation script automatically adds Rust to the system PATH after the next login.
– To start using Rust right away instead of restarting the terminal:
$ source $HOME/.cargo/env
OR
– Add the PATH to your bash file ( ~/.bash_profile )
$ export PATH=”$HOME/.cargo/bin:$PATH”
2. Version check
$ rustc –version
=> rustc x.y.z(abcabcabc yyyy-mm-dd)
3. Check the PATH system variable to ensure that RUST is present
$ echo $PATH
=> $HOME/.cargo/bin. ( You should see this directory in the PATH )
4. Rust Local Documentation
$ rustup doc
5. Updating Rust
$ rustup update
6. Uninstalling Rust and rustup
$ rustup self uninstall
Customary Hello World Program:
1. Creation of a project directory to store the Rust code
$ mkdir ~/RUST/projects
$ cd ~/RUST/projects
$ mkdir hello_world
$ cd hello_world/
2. Writing the first program
– Rust files always end with “.rs” extension
– File naming convention : abc_def_gh.rs
– Example: hello_world.rs
– Create a new src file : hello_world.rs
fn main() {
println!(“Hello world!”);
}
3. Compiling the code:
$ rustc hello_world.rs
4. Running the code:
$ ./hello_world
Parts of a Rust Program:
1. fn main() { } :
- Defines a function main in Rust. main function is special and it is the one that runs first in every Rust executable program
- Declaring a function named main that has no parameters and returns nothing
- Parameters if needed to be passed then they go inside the parentheses ()
- Function body is wrapped in curly brackets {}
2. println!(“Hello world!”);
- Calls a Rust macro
- Function call would have been println ( without !)
- Using ! implies calling a macro instead of a normal function
- String “Hello world!” is being passed as an argument to macro println!
- The line ends with a semicolon(;), similar to C language
3. rustc hello_world.rs
- Invoking Rust compiler on hello_world.rs to obtain the executable
- Generates hello_world executable file ( can be seen with “ls -l” command )
4. ./hello_world
– Running the executable code