Rust Basics for programming

Rust is not a particularly original language. Its design elements came from a wide range of sources.
 ▸ Abstract Machine Model : C
 ▸ Data types : C, SML, OCaml, Lisp, Limbo
 ▸ Optional Bindings : Swift
 ▸ Hygienic Macros : Scheme
 ▸ Functional Programming : Haskell, OCaml, F#
 ▸ Attributes : ECMA-335
 ▸ Memory Model and Memory Management : C++, ML Kit, Cyclone
 ▸ Type Classes : Haskell
 ▸ Crate : Assembly in the ECMA-335 CLI model
 ▸ Channels and Concurrency : Newsqueak, Alef, Limbo
 ▸ Message passing and Thread failure : Erlang


Rust files should have .rs file extension and if you’re using more than one word for the file name, use lower case and separate words with an underscore (snake_case)

To Compile
rustc file.rs



Cargo is Rust’s build-in Package Manager. But mainly it uses for,
 ▸ Create new project : cargo new
 ▸ Update dependencies : cargo update
 ▸ Build project : cargo build
 ▸ Build and run a project : cargo run
 ▸ Run tests : cargo test
 ▸ Generate documentation via rustdoc: cargo doc

A crate is a package. Crates can be shared via Cargo.
A crate can produce an executable or a library. In other words, it can be a binary crate or a library crate.
01. cargo new crate_name --bin : produces an executable
02. cargo new crate_name --lib OR cargo new crate_name : produces a library


.toml(capital c) is the configuration file which contains all of the metadata that Cargo needs to compile your project.

 ▸ Source code goes in the src directory.
 ▸ The default library file is src/lib.rs.
 ▸ The default executable file is src/main.rs.
 ▸ Other executables can be placed in src/bin/*.rs.


Programming Basics

//! Line comments; document the enclosing item
/*! Block comments; document the enclosing item !*/
/// This is a module comment

use //! to write crate and module-level documentation, nothing else. When using mod blocks, use /// outside of the block.

Rust is a statically typed language; It checks data type at compile time. But it doesn’t require you to actually type it when declare variable bindings.

let a = true;
let b: bool = true;

On Data Types of Bool and Chart we use the following for Numbers
i8 i16 i32 i64 : fixed size(bit) signed(+/-) integer types
u8 u16 u32 u64 : fixed size(bit) unsigned(+) integer types


 ▸ Functions are declared with the keyword fn
 ▸ When using arguments, you must declare data types.
 ▸ By default functions return empty tuple (). If you want to return a value, return type must be specified after ->

fn main() {
println!("Hello, world!");
}
//Function with arguments
fn print_sum(a: i8, b: i8) {
println!("sum is: {}", a + b);
}
//Returning
fn plus_one(a: i32) -> i32 {
a + 1 //no ; means an expression, return a+1
}


For Conditional statements

let team_size = 7;
if team_size < 5 {
println!("Small");
} else if team_size < 10 {
println!("Medium");
} else {
println!("Large");
}

For Loop Statements

let mut a = 1;
while a <= 10 {
println!("Current value : {}", a);
a += 1; //no ++ or -- in Rust
}

for b in 0..6 {
if b == 0 {
println!("Skip Value : {}", b);
continue;
} else if b == 2 {
println!("Break At : {}", b);
break;
}
println!("Current value : {}", b);
}





So we should be good with the basics
Variables
Functions
Conditional statements
Looping statements
Output to the screen



Comments

Popular posts from this blog

Solidity Hash of Structs (Testing hash uniqueness)

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Parity installation on Ubuntu Virtual Machine