Skip to content

Instantly share code, notes, and snippets.

@helpermethod
Last active November 23, 2021 20:34
Show Gist options
  • Save helpermethod/b65cccde13294496a9f3220d0e47d0b7 to your computer and use it in GitHub Desktop.
Save helpermethod/b65cccde13294496a9f3220d0e47d0b7 to your computer and use it in GitHub Desktop.
Rust 101
let s = String::from("Hello World!");
// move == transfering ownership
let t = s;
// mutability of data can be changed when ownership is transferred
let mut u = s;
let mut first_name = String::from("Oliver")
// creating a reference == borrowing
fn own(s: &String) {
println!("{}", s);
}
fn append_last_name(s: &mut String) {
s.push_str(" Weiler");
}
// ! An immutable references' scope ends the last time it is used
let me = String::from("Oliver Weiler");
let also_me = &me;
// scope of also_me ends here
println!("{}", also_me);
// a slice
let oliver: &s = &me[..6];
let weiler: &s = &me[6..];
// string literals are slices
let oliver_weiler = "Oliver Weiler";
// struct definition
struct Person {
first_name: String,
last_name: String,
age: u32,
}
// struct instantiation
let mut me = Person {
first_name: String::from("Oiver"),
last_name: String::from("Weiler"),
age: 41,
};
me.first_name = String::from("Oliver")
let first_name = "Kathrin";
let last_name = "Weiler";
let age = 36;
// field init shorthand
let kathrin = Person {
first_name,
last_name,
age,
};
// struct update
let clone = Person {
first_name = String::from("Kate"),
..kathrin
};
// tuple structs
struct Point(i32, i32, i32);
struct Color(i32, i32, i32);
let p = Point(0, 0, 0);
let c = Color(255, 255, 255);
struct Rectangle {
width: u32,
height: u32,
}
// add area method
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
let p = Person {
first_name: String::from("Oliver"),
last_name: String::from("Weiler"),
age: 41,
}
impl Person {
fn full_name(&self) -> String {
format!("{} {}", self.first_name, self.last_name)
}
// associated function
fn new_born(first_name: String, last_name: String) -> Person {
Person {
first_name,
last_name,
age: 0
}
}
}
// automatic referencing
println!("{}", p.full_name());
// equivalent to
println!("{}", &p.full_name());
enum Message {
ChangeColor(u8, u8, u8),
Echo(String),
// anonymous struct
Move { x: i32, y: i32 },
Quit
}
impl Message {
fn call(&self) {
println!("{:?}", &self);
}
}
struct State {
color: (u8, u8, u8),
position: Point,
quit: bool,
}
impl State {
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
}
fn quit(&mut self) {
self.quit = true;
}
fn echo(&self, s: String) {
println!("{}", s);
}
fn move_position(&mut self, p: Point) {
self.position = p;
}
pub fn process(&mut self, message: Message) {
match message {
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
Message::Echo(s) => self.echo(s),
Message::Move { x, y } => self.move_position(Point { x, y }),
Message::Quit => self.quit()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment