pub fn variable_scope() {
{
let s = "hello";
println!("{}", s) } }
pub fn string_type() {
let mut s = String::from("hello");
s.push_str(", world!");
println!("{}", s);
}
pub fn variable_move() {
stack_copy();
heap_move();
heap_copy();
}
fn heap_copy() {
let s1 = String::from("hello");
let s2 = s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);
}
fn heap_move() {
let s1 = String::from("hello");
let s2 = s1;
println!("s2: {}", s2);
}
fn stack_copy() {
let x = 5;
let y = x;
println!("x: {}", x);
println!("y: {}", y);
}
pub fn ownership_func() {
let s = String::from("hello");
takes_ownership(s);
let x = 5;
makes_copy(x); }
fn takes_ownership(some_string: String) {
println!("{}", some_string);
}
fn makes_copy(some_integer: i32) {
println!("{}", some_integer);
}
pub fn ownership_return_back() {
let s1 = gives_ownership();
let s2 = String::from("hello");
let s3 = takes_and_gives_back(s2); }
fn gives_ownership() -> String {
let some_string = String::from("yours");
some_string }
fn takes_and_gives_back(a_string: String) -> String {
a_string }
pub fn ownership_return_multi_values() {
let s1 = String::from("hello");
let (s2, len) = calculate_length(s1);
println!("The length of '{}' is {}.", s2, len);
}
fn calculate_length(s: String) -> (String, usize) {
let length = s.len();
(s, length)
}