-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66653cb
commit 41ca94f
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
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" | ||
|
||
|
This file contains 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
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); | ||
// } |
This file contains 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
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); | ||
// } | ||
|
This file contains 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
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); | ||
} |