Skip to content

Instantly share code, notes, and snippets.

View destrex271's full-sized avatar
🎯
Focusing

Akshat Jaimini destrex271

🎯
Focusing
View GitHub Profile
pub struct Person{
name: String
}
pub struct Cat{
name: String
}
pub trait Eat{
fn eat(&self);
}
impl Eat for Person{
// C-type standard struct
struct Song{
title: String,
album: String,
artist: String,
time_sec: i32
}
// Tuple Struct
struct Point(i32, i32, i32);
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
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
}
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}");
}