2025-01-02 14:12:46 -07:00

34 lines
831 B
Rust

use std::char;
fn main() {
println!("Hello, world!");
// ownership_ex();
borrowing_references();
}
fn ownership_transfer_ex () {
// this is a string literal
let s = "hello";
// this is a string
let ab = String::from("hello");
let a: char = ab.chars().nth(0).expect("REASON");
println!("{}", a); // outputs 'h'
// do stuff with s
}
// is is out of scope and therefore invalid
fn borrowing_references () {
let mut s1 = String::from("hello");
change_string(&mut s1);
// s1 is still valid aat this point so ownership didint change
println!("{}", s1);
// do more stuff with s1
}
fn change_string (some_string: &mut String) {
// using borrowed value contained in some_string
some_string.push_str(", world");
// some_string out of scope and therefore invalid
}