Skip to content

Commit 9a9278d

Browse files
committed
Add ownership example
1 parent 0b2af4d commit 9a9278d

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

ownership/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "ownership"
3+
version = "0.1.0"
4+
authors = ["Chanaka Fernando <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

ownership/src/main.rs

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
fn main() {
2+
println!("Hello, ownership!");
3+
4+
let mut s = String::from("hello");
5+
s.push_str(", world!");
6+
7+
println!("{}", s);
8+
9+
let s1 = String::from("Chanaka");
10+
// Cloning the data here because RUST does not allow accessing s1 after moving the ownership to s2
11+
let s2 = s1.clone();
12+
13+
println!("s1 is {} and s2 is {}", s1, s2);
14+
int_copy();
15+
16+
let a = String::from("Hello");
17+
takes_ownership(a);
18+
// The below line does not work because the ownership of "a" has been transferred to takes_ownership function
19+
//println!("{} World!", a);
20+
21+
let b = 5;
22+
makes_copy(b);
23+
println!("{} World!", b);
24+
25+
let s1 = gives_ownership();
26+
let s2 = String::from("hello");
27+
let s3 = takes_and_gives_back(s2);
28+
29+
let n = calculate_length(&s1);
30+
println!("The length of string {} is {}", s1, n);
31+
32+
let mut s = String::from("hello");
33+
change(&mut s);
34+
println!("Mutated string is {}", s);
35+
36+
let my_string = String::from("hello world");
37+
38+
// first_word works on slices of `String`s
39+
let word = first_word(&my_string[..]);
40+
41+
let my_string_literal = "hello world";
42+
43+
// first_word works on slices of string literals
44+
let word = first_word(&my_string_literal[..]);
45+
46+
// Because string literals *are* string slices already,
47+
// this works too, without the slice syntax!
48+
let word = first_word(my_string_literal);
49+
50+
println!("first word is {}", word);
51+
52+
}
53+
54+
fn int_copy() {
55+
println!("Starting the integer copy function");
56+
let x = 5;
57+
// Here we don't use clone since types stored in stack does not need the clone method
58+
let y = x;
59+
println!("The value of x is {}, the value of y is {}", x, y);
60+
}
61+
62+
fn takes_ownership(some_string: String) {
63+
println!("{}", some_string);
64+
}
65+
66+
fn makes_copy(some_integer: i32) {
67+
println!("{}", some_integer);
68+
}
69+
70+
fn gives_ownership() -> String {
71+
let some_string = String::from("hello");
72+
println!("from gives ownership {}", some_string);
73+
some_string
74+
}
75+
76+
fn takes_and_gives_back(a_string: String) -> String {
77+
println!("from takes and gives back {}", a_string);
78+
a_string
79+
}
80+
81+
fn calculate_length(s: &String) -> usize {
82+
s.len()
83+
}
84+
85+
fn change(some_string: &mut String) {
86+
some_string.push_str(", world");
87+
}
88+
89+
fn first_word(s: &str) -> &str {
90+
let bytes = s.as_bytes();
91+
92+
for (i, &item) in bytes.iter().enumerate() {
93+
if item == b' ' {
94+
return &s[0..i];
95+
}
96+
}
97+
&s[..]
98+
}

0 commit comments

Comments
 (0)