Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AlyceOsbourne authored Feb 6, 2024
1 parent 66653cb commit 41ca94f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 2024/rust_mem_model/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 2024/rust_mem_model/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "rust_mem_model"
version = "0.1.0"
edition = "2021"


40 changes: 40 additions & 0 deletions 2024/rust_mem_model/src/step_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

// fn main() {
// let s1 = String::from("hello");
// let s2 = s1;
// println!("{}, world!", s1);
// }

// fn main() {
// let s1 = String::from("hello");
// let s2 = &s1;
// println!("{}, world!", s2);
// }

// fn main() {
// let mut s1 = String::from("hello");
// let s2 = &mut s1;
// s2.push_str(", world!");
// println!("{}", s1);
// }

// fn main() {
// let mut s1 = String::from("hello");
// let s2 = &mut s1;
// let s3 = &mut s1;
// s2.push_str(", world!");
// println!("{}", s1);
// }
//

// fn main() {
// let s1 = String::from("hello");
// let s2 = s1.clone();
// println!("{}, world!", s1);
// }

// fn main() {
// let s1 = String::from("hello, world!");
// let s2 = &s1[0..5];
// println!("{}", s2);
// }
17 changes: 17 additions & 0 deletions 2024/rust_mem_model/src/step_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

// fn main() {
// // stack allocation
// let x = 5;
// let y = x;
//
// println!("x = {}, y = {}", x, y);
// }

// fn main() {
// // heap allocation
// let s1 = String::from("hello");
// let s2 = s1;
//
// println!("{}, world!", s1);
// }

20 changes: 20 additions & 0 deletions 2024/rust_mem_model/src/step_3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod main;

fn same_lifetime<'a>(s: &'a str, start: usize, end: usize) -> &'a str {
&s[start..end]
}

fn equal_lifetimes<'a, 'b: 'a>(x: &'a str, y: &'b str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}

fn main() {
let s1 = String::from("hello, world!");
let s2 = same_lifetime(s1.as_str(), 0, 5);
let result = equal_lifetimes(s1.as_str(), s2);
println!("The longest string is {}", result);
}

0 comments on commit 41ca94f

Please sign in to comment.