ownership, references
This commit is contained in:
8
ownership/Cargo.toml
Normal file
8
ownership/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "ownership"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
43
ownership/src/main.rs
Normal file
43
ownership/src/main.rs
Normal file
@ -0,0 +1,43 @@
|
||||
fn main() {
|
||||
let mut s = String::from("farts");
|
||||
let s2 = String::from("farts");
|
||||
s.push_str(" and blarts");
|
||||
println!("Hello, {s}!");
|
||||
|
||||
s = String::from("hello");
|
||||
takes_ownership(s.clone());
|
||||
takes_ownership(s.clone());
|
||||
let r1 = &mut s;
|
||||
takes_ref(r1);
|
||||
let r2 = &mut s;
|
||||
takes_ref(r2);
|
||||
//takes_ref(r1);
|
||||
let x = 5;
|
||||
makes_copy(x);
|
||||
let s3 = take_and_give_back(s);
|
||||
println!("cha cha cha {}", s3);
|
||||
let s4 = take_and_give_back(String::from("farts"));
|
||||
let s5 = take_and_give_back(s2);
|
||||
|
||||
|
||||
println!("cha cha cha {} {}", s4, s5);
|
||||
|
||||
}
|
||||
|
||||
fn take_and_give_back(mut some_string: String) -> String {
|
||||
some_string.push_str("caflobble");
|
||||
some_string
|
||||
}
|
||||
|
||||
fn takes_ref(some_string: &mut String) {
|
||||
some_string.push_str("sdf");
|
||||
println!("{}", some_string);
|
||||
}
|
||||
|
||||
fn takes_ownership(some_string: String) {
|
||||
println!("{}", some_string);
|
||||
}
|
||||
|
||||
fn makes_copy(some_int: u8) {
|
||||
println!("some int: {}", some_int);
|
||||
}
|
Reference in New Issue
Block a user