Last active
November 17, 2024 18:09
-
-
Save dacr/c353f424a2ef974b0ef7ca32edfb4b49 to your computer and use it in GitHub Desktop.
hello rust memory / published by https://github.com/dacr/code-examples-manager #4a8147eb-520e-4658-8250-bbcf987294e2/7e9823d761e70f00ab18ee1138ad3940e55c31dc
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
#!/usr/bin/env rust-script | |
// summary : hello rust memory | |
// keywords : rust, memory, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 4a8147eb-520e-4658-8250-bbcf987294e2 | |
// created-on : 2024-10-14T21:27:38+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : ./$file | |
// ------------------------------------------------------------------------------------------------- | |
fn mem10() { | |
let str0 = "truc"; // immutable string on the stack | |
let str1 = String::from("Hello"); // dynamic string on the heap | |
println!("mem1: str1={str1} - str1 has ownership"); | |
let mut str2 = str1; | |
// ownership has moved str1 -> str2 / string data (pointer/len/capacity) is copied but not the content : | |
str2.push_str("๐"); | |
println!("mem1: str0={str0} str2={str2} - str1 has lost ownership, it is no longer accessible"); | |
} | |
// ------------------------------------------------------------------------------------------------- | |
fn mem15_capitalize(input: String) -> String { | |
input.to_uppercase() // new string is created | |
} | |
fn mem15() { | |
let s1 = String::from("hello ๐"); | |
let s2 = mem15_capitalize(s1); // From here s1 is no longer accessible : ownership as moved | |
println!("capitalized: {s2}") | |
} | |
// ------------------------------------------------------------------------------------------------- | |
fn mem16_identity(input: String) -> String { | |
input | |
} | |
fn mem16() { | |
let s1 = String::from("hello ๐"); | |
let s2 = mem16_identity(s1); // s1 --movedTo-> mem16_identity --movedTo-> s2 | |
println!("transferred s1->s2: {s2} - s2 is s1 in fact - ownership back into s2 after the call") | |
} | |
// ------------------------------------------------------------------------------------------------- | |
fn mem17_length(input: String) -> (String, usize) { | |
let len = input.len(); | |
// (input, input.len()) // TO AVOID : (value moved here, value borrowed here after move) compilation error | |
// 2 steps is required to avoid borrowing issue !! | |
(input, len) | |
} | |
fn mem17() { | |
let s1 = String::from("hello ๐"); | |
let (s2, l) = mem17_length(s1); // s1 --movedTo-> mem17_length --movedTo-> s2 | |
println!("transferred: {s2} of len {l} - s2 is s1 in fact - ownership back into s2 after the call") | |
} | |
// ------------------------------------------------------------------------------------------------- | |
fn mem20_capitalize(input: &String) -> String { | |
// no ownership transfer for input, we just borrow the string and give it back once used | |
input.to_uppercase() | |
} | |
fn mem20() { | |
let s1 = String::from("hello ๐"); | |
let s2 = mem20_capitalize(&s1); // ownership doesnยดt move | |
println!("capitalized: {s1} {s2} using a reference as parameter, creating a reference == borrowing ") | |
} | |
// ------------------------------------------------------------------------------------------------- | |
fn mem40() { | |
let s1 = String::from("hello ๐"); // one memory zone on the heap = 1 owner only | |
//let s2 = s1; // ownership is transfered! | |
//println!("s1 = {}, s2 = {}", s1, s2); // COMPILE error - "hello ๐" doesn't belong to s1 anymore | |
let s3 = s1.clone(); | |
println!("s1 = {}, s3 = {}", s1, s3); // CAN'T COMPILE - s1 ne possรจde plus "hello ๐" | |
// โ ๏ธ s1 has been copied into a new memory zone on the heap | |
} | |
// ------------------------------------------------------------------------------------------------- | |
struct TrucDroppable { // By default, in DROP mode | |
age: u16, | |
} | |
#[derive(Copy, Clone)] | |
struct TrucCopiable { // NOW configured in COPY mode | |
age: u16, | |
} | |
fn mem80() { | |
let tc1: TrucCopiable = TrucCopiable { age: 42 }; | |
let tc2 = tc1; // COPIED | |
println!("mem2: tc1={} tc2={}", tc1.age, tc2.age); | |
let td1: TrucDroppable = TrucDroppable { age: 42 }; | |
let td2 = td1; // MOVED - td1 is no longer accessible | |
//println!("mem2: td1={} td2={}", td1.age, td2.age); | |
println!("mem2: td2={}", td2.age); | |
} | |
// ------------------------------------------------------------------------------------------------- | |
fn main() { | |
mem10(); | |
mem15(); | |
mem16(); | |
mem17(); | |
mem20(); | |
mem40(); | |
mem80(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment