Garbage Collection
None - frees up memory automatically
Cargo
cargo new demoName cargo build cargo build --release cargo run --release cargo watch -x run
Scalar Types: integer, floating point numbers, boolean, , characters
large whole number, floating point = decimals, boolean: true/false; characters:
tuple type
(5, 5.0, “chris”). let (x, y, z) = tuple | x = 5
fn hello_world(name: &str) {
println!("hello {}", name);
}need & to know length of string else it wont compile
i8
8 bit integer
::
bind method to call that library
u64
unsigned integer, number cant be negative
float
f32 floating number 6.7 with decimal
if else statement
if n < 30 {} else {} | ==; !=; ;
enum
enum Name {variance} eg {Up, Down}. let test:Name = Name::Up
constants
const Name: u8 = 20; name and type. for n in 1..Name {}
tuples
bunch of variables in 1 place. let tup1 = (20, “test”, 21.1);
code block
fn main(){
let x = 10;
{inside here can access outside x
}
outside here cant access inside {}
}shadowing
let mut x = 10;
{ let x = 15; }
x is 10 here. Inside code block is isolated.references
another way to refer to a variable. eg domenic is also dom.
let mut x = 10; let xr = &x; cant change xr though.
eg let dom = &mut x;
*dom += 1; can now change dom.
{eg let dom = &mut x;
*dom += 1; can now change dom.}structs
group similar info in same place
struct Color {
red: u8,
green: u8,
blue: u8
}
fn main(){
let bg = Color {red: 255, green: 70, blue: 15};tuple struct
struct Color(u8, u8, u8);
fn main(){
let red = Color(255, 0, 0);
println!("{}", red.0, red.1, red.2);Pass by reference
struct Color {red: u8, green: u8, blue: u8}
fn main(){let blue = Color{red: 0, green: 0, blue: 255};
print_color(&blue);
}
fn print_color(c: &Color){
println!("Color - R:{} G:{}, B{}", c.red, c.green, c.blue);}
& means reference to itImpl Keyword
struct Rectangle {width: u32, height: u32}
impl Rectangle {
fn print_description(&self) // &self gives access to itself
println!("Rectangle: {} x {}}, self.width, self.height); }
fn is_square(&self) -> bool {self.width == self.height }
}
fn main(){ let my_rect = Rectangle {width: 10, height: 5};
my_rect.print_description(); // no need to pass in anything as its done automatically in &self
println!("Retangle is a squre: {}", my_rect.is_square());
}String
fn main(){
let my_string = String::from("This is a string"); // variable my_string has data type String
}Traits
Something like an object, struct etc can do
struct Person {name: String, age: u8}
impl ToString for Person {
fn to_string(&self) -> String {
return format!("My name is {} and I am {}.}, self.name, self.age);
fn main(){
let dom = Person {name: String::from("Domenic"), age: 21};
println!("{}", dom.to_string()); }Vector
let my_vector = vec![1, 2, 3, 4];
Defining Traits
trail certain set of rules/requirements, for that object/struct must have in order to have that name of the trait
struct Person {name: String, age: u8}
trait HasVoiceBox {
// speak
fn speak(&self); // if you have a voice box, you can speak
// Check if can speak
fn can_speak(&self) -> bool; }
// implement this trait on Person struct
impl HasVoiceBox for Person {
fn speak(&self) {
println!("Hello my name is {}", self.name);
}
fn can_speak(&self) -> bool{ if self.age > 0 {return true;} retnr false; }
}
}
fn main(){
let person = Person{name: String::from("Bob"), age: 41 }
println!("Can {} speak? {}", person.name, person.can_speak());
person.speak();
}