This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct Person{ | |
name: String | |
} | |
pub struct Cat{ | |
name: String | |
} | |
pub trait Eat{ | |
fn eat(&self); | |
} | |
impl Eat for Person{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C-type standard struct | |
struct Song{ | |
title: String, | |
album: String, | |
artist: String, | |
time_sec: i32 | |
} | |
// Tuple Struct | |
struct Point(i32, i32, i32); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut x = String::from("Hello World!"); | |
// print(x.clone()); Creates copy, prevents ownership transfer but consumes memory | |
print(&x); // Pass a 'reference' to the variable x on the stack as well as the heap and therefor ownership is not passed to the print function | |
// We can also send a 'mutable' reference, if we want our function to make changes to the current value | |
change(&mut x); | |
// While borrowing a variable you can have either: | |
// 1. A single mutable referrence | |
// 2. A number of immutable references | |
// And references must always be valid, like if a var is a ref to another var then if the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main(){ | |
let x = String::from("test"); | |
let y = x; | |
// You cannot access X after this as the ownership of x has been passed to y | |
// Instead you can either clone or borrow the value | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let x = String::from("Hello World!"); | |
println!("{x}"); | |
print(x); | |
// println!("{x}"); // program panics for types like string as they do not implement the copy trait | |
} | |
fn print(string: String)->(){ | |
println!("{string}"); | |
} |