Rust: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 23: | Line 23: | ||
cargo run | cargo run | ||
= Fundamental Data Types = | = Types and Variables= | ||
== Primitive types == | |||
== Fundamental Data Types == | |||
=== Primitive types === | |||
Cam declare with size of type | Cam declare with size of type | ||
let a:u8 = 123; // unsigned int 8 bits number immutable | let a:u8 = 123; // unsigned int 8 bits number immutable | ||
Line 34: | Line 36: | ||
Now variable based on OS e.g. | Now variable based on OS e.g. | ||
let z:isize = 123 // signed 64 bit if on 64 bit OS | let z:isize = 123 // signed 64 bit if on 64 bit OS | ||
== Decimal == | === Decimal === | ||
let e:f64 = 2.5 // double-precision, 8 bytes or 64-bits | let e:f64 = 2.5 // double-precision, 8 bytes or 64-bits | ||
== Char == | === Char === | ||
let x:char = 'x' // Note 4 bytes unicode | let x:char = 'x' // Note 4 bytes unicode | ||
== boolean == | === boolean === | ||
let g:bool = false; // Note 4 bytes unicode | let g:bool = false; // Note 4 bytes unicode | ||
= Operators = | == Operators == | ||
Does not support -- and ++ but does support | Does not support -- and ++ but does support | ||
a -= 2; | a -= 2; | ||
Line 53: | Line 55: | ||
let pi_less_4 = std::f64::consts::PI < 4.0; // true | let pi_less_4 = std::f64::consts::PI < 4.0; // true | ||
= Scope and shadowing = | == Scope and shadowing == | ||
Curly braces keep scope | Curly braces keep scope | ||
<syntaxhighlight lang="toml"> | <syntaxhighlight lang="toml"> | ||
Line 77: | Line 79: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Constants = | == Constants == | ||
Standard const | Standard const | ||
<syntaxhighlight lang="rust"> | <syntaxhighlight lang="rust"> | ||
Line 87: | Line 89: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Stack and Heap = | == Stack and Heap == | ||
Same a c++ i.e. | Same a c++ i.e. | ||
<syntaxhighlight lang="rust"> | <syntaxhighlight lang="rust"> |
Revision as of 03:14, 22 June 2020
Sample program
fn main() {
println!("Hello, world!");
}
Cargo
Sample file
[package]
name = "hello_world"
version = "0.0.1"
authors = [ "Iain Wiseman iwiseman@bibble.co.nz" ]
Sample commands
cargo new hello_world --bin cargo build cargo run
Types and Variables
Fundamental Data Types
Primitive types
Cam declare with size of type
let a:u8 = 123; // unsigned int 8 bits number immutable let a:i8 = 123; // signed int 8 bits number immutable let mut a:u8 = 123; // unsigned int 8 bits number mutable
Or without e.g.
let mut c = 123456789 // 32-bit signed i32 println!("c = {}", c);
Now variable based on OS e.g.
let z:isize = 123 // signed 64 bit if on 64 bit OS
Decimal
let e:f64 = 2.5 // double-precision, 8 bytes or 64-bits
Char
let x:char = 'x' // Note 4 bytes unicode
boolean
let g:bool = false; // Note 4 bytes unicode
Operators
Does not support -- and ++ but does support
a -= 2;
Remainder can be calculated using
a%3
Bitwise
let c = 1 | 2 // | OR
Shift
let two_to_10 = 1 << 10; // 1024
Logical of standard e.g.
let pi_less_4 = std::f64::consts::PI < 4.0; // true
Scope and shadowing
Curly braces keep scope
fn test()
{
{
let a = 5;
}
println!("Broken {a}");
}
Shadowing is fine though
fn test()
{
let a = 5;
{
let a = 10;
println!("10 {a}");
}
println!("5 {a}");
}
Constants
Standard const
const MEANING_OF_LIFE:u8 = 42;
Static const
static Z:i32 = 123;
Stack and Heap
Same a c++ i.e.
let y = Box::new(10);
println!("y = {}", *y);