summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2020-08-12 16:15:01 +0800
committerJordan Gong <jordan.gong@protonmail.com>2020-08-12 16:15:01 +0800
commit0cebf66a1816e5bf3c005dff6fb17bba55c1517f (patch)
tree2ce3d2e3c05a7ccb980367312cc170c95a517583
parent4385163bf4809b27cf2576ad5cc379571d4264cf (diff)
Understand owership
-rw-r--r--owership/.gitignore18
-rw-r--r--owership/Cargo.toml9
-rw-r--r--owership/src/main.rs139
3 files changed, 166 insertions, 0 deletions
diff --git a/owership/.gitignore b/owership/.gitignore
new file mode 100644
index 0000000..e629269
--- /dev/null
+++ b/owership/.gitignore
@@ -0,0 +1,18 @@
+
+# Created by https://www.toptal.com/developers/gitignore/api/rust
+# Edit at https://www.toptal.com/developers/gitignore?templates=rust
+
+### Rust ###
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
+# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
+Cargo.lock
+
+# These are backup files generated by rustfmt
+**/*.rs.bk
+
+# End of https://www.toptal.com/developers/gitignore/api/rust
+
diff --git a/owership/Cargo.toml b/owership/Cargo.toml
new file mode 100644
index 0000000..41a65ec
--- /dev/null
+++ b/owership/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "owership"
+version = "0.1.0"
+authors = ["Jordan Gong <jordan.gong@protonmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/owership/src/main.rs b/owership/src/main.rs
new file mode 100644
index 0000000..d2bd765
--- /dev/null
+++ b/owership/src/main.rs
@@ -0,0 +1,139 @@
+fn main() {
+ /* heap data deep copy */
+ let s1 = String::from("Hello");
+ let s2 = s1.clone();
+
+ println!("s1 = {}, s2 = {}", s1, s2);
+
+ /* stack data copy */
+ let x = 5;
+ let y = x;
+
+ println!("x = {}, y = {}", x, y);
+
+ /* functions */
+ let s = String::from("hello");
+
+ takes_owership(s);
+
+ let x = 5;
+
+ makes_copy(x);
+
+ /* return values and scope */
+ let s1 = gives_owership();
+
+ let s2 = String::from("hello");
+
+ let s3 = takes_and_gives_back(s2);
+
+ /* parameters */
+ let s1 = String::from("hello");
+
+ let (s2, len) = calculate_length(s1);
+
+ println!("The length of '{}' is {}.", s2, len);
+
+ /* reference(borrowing) */
+ let s1 = String::from("hello");
+
+ let len = calculate_length_ref(&s1);
+
+ println!("The length of '{}' is {}.", s1, len);
+
+ /* mutable reference */
+ let mut s = String::from("hello");
+
+ change(&mut s);
+
+ /* mutable and immutable */
+ let mut s = String::from("hello");
+
+ let r1 = &s;
+ let r2 = &s;
+ println!("{} and {}", r1, r2);
+
+ let r3 = &mut s;
+ println!("{}", r3);
+
+ /* slice */
+ let mut s = String::from("hello world");
+
+ let word = first_word_index(&s);
+
+ s.clear();
+
+ println!("the first word index: 0-{}", word);
+
+ /* string slice */
+ let s = String::from("hello world");
+
+ let hello = &s[0..5];
+ let world = &s[6..11];
+
+ println!("{} {}", hello, world);
+
+ /* slice first word */
+ let mut s = String::from("hello world");
+
+ let word = first_word(&s);
+
+ println!("the first word: {}", word);
+
+ s.clear();
+}
+
+fn takes_owership(some_string: String) {
+ println!("{}", some_string);
+}
+
+fn makes_copy(some_integer: i32) {
+ println!("{}", some_integer);
+}
+
+fn gives_owership() -> String {
+ let some_string = String::from("hello");
+ some_string
+}
+
+fn takes_and_gives_back(a_string: String) -> String {
+ a_string
+}
+
+fn calculate_length(s: String) -> (String, usize) {
+ let length = s.len();
+
+ (s, length)
+}
+
+fn calculate_length_ref(s: &String) -> usize {
+ s.len()
+}
+
+fn change(some_string: &mut String) {
+ some_string.push_str(", world!");
+}
+
+fn first_word_index(s: &String) -> usize {
+ let bytes = s.as_bytes();
+
+ for (i, &item) in bytes.iter().enumerate() {
+ if item == b' ' {
+ return i;
+ }
+ }
+
+ s.len()
+}
+
+fn first_word(s: &String) -> &str {
+ let bytes = s.as_bytes();
+
+ for (i, &item) in bytes.iter().enumerate() {
+ if item == b' ' {
+ return &s[0..i];
+ }
+ }
+
+ &s[..]
+}