Rust: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 171: Line 171:


= Data Structures =
= Data Structures =
== Structs ==
<syntaxhighlight lang="rust">
struct Point
{
  x: f64,
  y: f64
}
fn main()
{
  let p = Point { x: 30.0, y: 4.0 };
  println!("point is at ({},{})", p.x, p.y)
}
</syntaxhighlight>
== Enumerators ==
=== Similar to c++ ===
<syntaxhighlight lang="rust">
enum Color {
  Red,
  Green,
  Blue
}
fn main()
{
  let c:Color = Color::Red;
  match c
  {
    Color::Red => prinln!("Color is Red");
    Color::Green => prinln!("Color is Green");
  }
}
</syntaxhighlight>
=== But maybe not we can add types ===
<syntaxhighlight lang="rust">
enum Color {
  Red,
  Green,
  Blue,
  RgbColor(u8,u8,u8) // Tuple
  CmykColor{cyan:u8, magenta:u8, yellow:u8, black:u8,} // Struct
}
fn main()
{
  let c:Color = Color::RgbColor(10,0.0);
  match c
  {
    Color::Red => prinln!("Color is Red");
    Color::Green => prinln!("Color is Green");
    Color::RgbColor(0,0,0) => prinln!("Color is Black");
    Color::RgbColor(r,g,b) => prinln!("Color is {},{},{}", r,g,b);
  }
  let d:Color = Color::CmykColor(cyan:0, magenta:0, yellow:0, black:0);
  match d
  {
    Color::Red => prinln!("Color is Red");
    Color::Green => prinln!("Color is Green");
    Color::RgbColor(0,0,0) => prinln!("Color is Black");
    Color::CmykColor(cyan:_, magenta:_, yellow:_, black:255) => prinln!("Black");
  }
}
</syntaxhighlight>
== Option <T> and if let ==
Used to avoid null or invalid values
<syntaxhighlight lang="rust">
let x = 3.0
let y = 0.0 // Divide by zero
let result:Option<f64> =
  if y != 0.0 { Some(x/y) } else { None };
// Using match
match result
{
  Some(z) => println!("Goody result"),
  None => println!("No result)
}
// Using if let
if let Some(z) = result { println!("z = {}", z); }
</syntaxhighlight>
== Arrays ==
<syntaxhighlight lang="rust">
<syntaxhighlight lang="rust">
</syntaxhighlight>
</syntaxhighlight>

Revision as of 04:24, 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);

Control Flow

if statement

Same as C++ except no brackets

 if temp > 30 
 {
    println!("Blah");
 }
 else if temp < 10 
 {
    println!("Blah"); 
 }
 else
 {
    println!("Blah"); 
 }

Elvis is like

  let a = if temp > 30 {"sunny"} else {"cloud"}

While and Loop

While

Same as C++ except no brackets

 while x < 1000
 {
 }

There is support for continue and break

Loop

Loop is while true

 loop
 {
    if y == 1 << 10 { break; }
 }

For Loop

A bit like kotlin loops (I think)

 for x in 1..11
 {
    println!("x = {}",x);
 }

You can get position in series as well

 for (pos,x) in (1..11).enumerate()
 {
    println!("x = {}, pos = {}",x, pos);
 }

Match

Match can be used like case

 let country = match country_code
 {
    44 => "uk",
    46 => "sweden",
    7 => "russia"
    1...999 => "unknown" // other triple dot inclusive
    _ => "invalid" // invalid
 };

Data Structures

Structs

struct Point
{
  x: f64,
  y: f64
}

fn main()
{
  let p = Point { x: 30.0, y: 4.0 };
  println!("point is at ({},{})", p.x, p.y)
}

Enumerators

Similar to c++

enum Color {
  Red,
  Green,
  Blue
}

fn main()
{
  let c:Color = Color::Red;
  match c
  {
     Color::Red => prinln!("Color is Red");
     Color::Green => prinln!("Color is Green");
  }
}

But maybe not we can add types

enum Color {
  Red,
  Green,
  Blue,
  RgbColor(u8,u8,u8) // Tuple
  CmykColor{cyan:u8, magenta:u8, yellow:u8, black:u8,} // Struct
}

fn main()
{
  let c:Color = Color::RgbColor(10,0.0);
  match c
  {
     Color::Red => prinln!("Color is Red");
     Color::Green => prinln!("Color is Green");
     Color::RgbColor(0,0,0) => prinln!("Color is Black");
     Color::RgbColor(r,g,b) => prinln!("Color is {},{},{}", r,g,b);
  }

  let d:Color = Color::CmykColor(cyan:0, magenta:0, yellow:0, black:0);
  match d
  {
     Color::Red => prinln!("Color is Red");
     Color::Green => prinln!("Color is Green");
     Color::RgbColor(0,0,0) => prinln!("Color is Black");
     Color::CmykColor(cyan:_, magenta:_, yellow:_, black:255) => prinln!("Black");
  }
}

Option <T> and if let

Used to avoid null or invalid values

let x = 3.0
let y = 0.0 // Divide by zero

let result:Option<f64> = 
   if y != 0.0 { Some(x/y) } else { None };

// Using match
match result
{
   Some(z) => println!("Goody result"),
   None => println!("No result)
}

// Using if let
if let Some(z) = result { println!("z = {}", z); }

Arrays