Skip to content

Instantly share code, notes, and snippets.

@andyjsbell
Created February 5, 2020 14:55
Show Gist options
  • Save andyjsbell/00afb77f9e87e3854e525cb69b739300 to your computer and use it in GitHub Desktop.
Save andyjsbell/00afb77f9e87e3854e525cb69b739300 to your computer and use it in GitHub Desktop.
Rust basics
enum Position {
Left,
Right,
Center,
Bottom
}
enum Clock {
Sundial{hour: u8},
Digital{hour: u8, min:u8},
Analog{hour: u8, min:u8, sec:u8},
}
#[derive(Debug)]
enum Colour {
Green(u32),
Blue(u32),
Red(u32),
Black(u32),
White(u32)
}
// Struct
#[derive(Debug)]
struct Car {
model: String,
colour: Colour,
}
// Tuple struct
struct Triangle(u32, u32, u32);
struct Length(u32);
impl Car {
// Only reads (&self)
fn add_distance(&self, len:Length) {
println!("distance {0} for model {1}", len.0, self.model);
}
fn turn_blue(&mut self) {
self.colour = Colour::Blue(0x0000ff00);
}
fn new() -> Car {
Car {
model: String::from("toyota"),
colour: Colour::Blue(0x0000ff00),
}
}
}
fn set_position(pos:Position) {
match pos {
Position::Left => {
println!("We moved left");
}
_ => {
println!("Something else");
}
}
}
fn main() {
// Variables
let x = 5;
println!("{}", x);
let a = true;
// Conditions
if a {
println!("True");
}
if x == 5 {
println!("x is 5");
}
// Tuples
let tup = (100, 'a', true);
println!("{}", tup.1);
let (first, second, third) = tup;
println!("{0}, {1}, {2}", first, second, third);
// Arrays
let array = [1,2,3];
println!("{}", array[2]);
let mut array1 = [1,2,3];
array1[2] = 4;
println!("{}", array1[2]);
println!("{:?}", array);
// Slice
let b = &array[0..1];
println!("{}", b[0]);
let amount = if b[0] == 1 {
50
} else {
0
};
println!("{}", amount);
// Loops
loop {
// forever
break;
}
// while
while amount == 50 {
break;
}
// for with range but could be a collection such as array or tuple?
for i in 1..11 {
println!("{}", i);
}
// We can iterate a slice of an array
for t in &array {
println!("array = {}", t);
}
// Cannot iterate a tuple
// for t in tup {
// }
let d1 = 1;
let d2 = 2;
match (d1, d2) {
(1, 1) => println!("Shouldn't be here"),
(1, _) | (_, 1) => println!("We have a one"),
_ => println!("Fallback"),
}
let car = Car {
model : String::from("ford"),
colour : Colour::Red(0xff000000),
};
println!("{}", car.model);
car.add_distance(Length(100));
let mut car1 = Car::new();
println!("{}", car1.model);
car1.turn_blue();
let word = String::from("roger");
say_something(word);
// println!("{}", word); // Error as ownership has moved to say_something
// to fix this use clone, use more memory but simple solution
let word1 = String::from("that");
say_something(word1.clone());
println!("{}", word1);
let s = String::from("book");
println!("s1={}", pluralize(s.clone()));
println!("s1={}", pluralize2(&s));
}
fn pluralize(mut s:String) -> String {
s.push_str("s");
s
}
// Borrow the value with a slice
fn pluralize2(s:&str) -> String {
s.to_owned() + "s"
}
fn say_something(word:String) {
println!("{}", word);
}
fn add_length(len:Length) -> Length {
len
}
fn add(num:i32, num1:i32) -> i32 {
num + num1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment