├── README.md ├── addlib ├── .gitignore ├── Cargo.toml └── src │ └── lib.rs ├── hello_world ├── .gitignore ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs ├── lifetimes ├── .gitignore ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs ├── multi_threads ├── .gitignore ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs ├── ownership ├── .gitignore ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs ├── rand_nr ├── .gitignore ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs ├── safe_mem ├── .gitignore ├── Cargo.lock ├── Cargo.toml └── src │ └── main.rs └── smart_pointer ├── .gitignore ├── Cargo.lock ├── Cargo.toml └── src └── main.rs /README.md: -------------------------------------------------------------------------------- 1 | # RustTraining 2 | 分享Rust培训代码. 3 | -------------------------------------------------------------------------------- /addlib/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /addlib/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "addlib" 3 | version = "0.1.0" 4 | authors = ["fgao"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /addlib/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod tests { 3 | #[test] 4 | fn it_works() { 5 | assert_eq!(2 + 2, 4); 6 | } 7 | 8 | #[test] 9 | fn test_add() { 10 | assert_eq!(4, super::add(3, 1)); 11 | assert_ne!(3, super::add(2, 2)); 12 | } 13 | } 14 | 15 | /// Usage: calculate two integer's sum 16 | pub fn add(a:i32, b:i32) -> i32 { 17 | a+b 18 | } 19 | -------------------------------------------------------------------------------- /hello_world/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /hello_world/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "hello_world" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /hello_world/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "hello_world" 3 | version = "0.1.0" 4 | authors = ["fgao"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /hello_world/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | -------------------------------------------------------------------------------- /lifetimes/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /lifetimes/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "lifetimes" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /lifetimes/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lifetimes" 3 | version = "0.1.0" 4 | authors = ["fgao"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /lifetimes/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | // implicit lifetime 3 | println!("*********************** example1 *****************************"); 4 | 5 | /* 6 | let r; 7 | { 8 | let x = 1; 9 | r = &x; 10 | } 11 | println!("r is {}", r); 12 | */ 13 | 14 | // explicit lifetime 15 | println!("*********************** example2 *****************************"); 16 | let str1 = String::from("long_str"); 17 | { 18 | let str2 = String::from("short"); 19 | //let result = longer_str(&str1, &str2); 20 | let result = longer_str2(&str1, &str2); 21 | 22 | println!("result is {}", result); 23 | } 24 | 25 | // explicit lifetime check 1 26 | println!("*********************** example3 *****************************"); 27 | /* 28 | let result; 29 | { 30 | let str2 = String::from("short"); 31 | result = longer_str2(&str1, &str2); 32 | } 33 | println!("result is {}", result); 34 | */ 35 | 36 | /* different lifetime */ 37 | println!("*********************** example4 *****************************"); 38 | let result2; 39 | { 40 | let str2 = String::from("short"); 41 | result2 = first_str(&str1, &str2); 42 | } 43 | println!("result2 is {}", result2); 44 | 45 | /* struct lifetime */ 46 | println!("*********************** example5 *****************************"); 47 | #[derive(Debug)] 48 | struct LifeStruct <'a> { 49 | x: &'a i32, 50 | } 51 | 52 | let x1 = 5; 53 | let mut ls = LifeStruct {x: &x1}; 54 | /* 55 | { 56 | let x2 = 6; 57 | ls.x = &x2; 58 | } 59 | */ 60 | println!{"ls is {:?}", ls}; 61 | 62 | /* static lifetime */ 63 | println!("*********************** example6 *****************************"); 64 | let s1 = "hello"; 65 | let mut ret = static_str(&s1); 66 | println!("ret is {}", ret); 67 | /* 68 | let s = String::from("hello2"); 69 | ret = static_str(&s); 70 | println!("ret is {}", ret); 71 | */ 72 | } 73 | 74 | /* 75 | fn longer_str(x:&String, y:&String) -> &String { 76 | if x.len() > y.len() { 77 | x 78 | } else { 79 | y 80 | } 81 | } 82 | */ 83 | 84 | 85 | fn longer_str2<'a>(x:&'a String, y:&'a String) -> &'a String { 86 | if x.len() > y.len() { 87 | x 88 | } else { 89 | y 90 | } 91 | } 92 | 93 | fn first_str<'a, 'b>(x:&'a String, _y:&'b String) -> &'a String { 94 | x 95 | } 96 | 97 | fn static_str(s:&'static str) -> &str { 98 | s 99 | } 100 | -------------------------------------------------------------------------------- /multi_threads/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /multi_threads/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "multi_threads" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /multi_threads/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "multi_threads" 3 | version = "0.1.0" 4 | authors = ["fgao"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /multi_threads/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::thread; 2 | use std::sync::Arc; 3 | use std::sync::mpsc; 4 | use std::time::Duration; 5 | use std::sync::Mutex; 6 | 7 | fn main() { 8 | let v = vec![1, 2, 3]; 9 | println!("vec is {:?}", v); 10 | 11 | /* ownership check */ 12 | println!("*********************** example1 *****************************"); 13 | //let thr = thread::spawn({ 14 | let thr = thread::spawn(move || { 15 | println!("thread print vec is {:?}", v); 16 | }); 17 | 18 | //println!("main pring vec {:?} again", v); 19 | thr.join().unwrap(); 20 | 21 | /* channel & ownership */ 22 | println!("*********************** example2 *****************************"); 23 | let (tx, rx) = mpsc::channel(); 24 | let tx1 = mpsc::Sender::clone(&tx); 25 | thread::spawn(move || { 26 | let val = String::from("Hi, i'm a thread"); 27 | tx1.send(val).unwrap(); 28 | //println!("thread: now val is {}", val); 29 | }); 30 | 31 | let val = rx.recv().unwrap(); 32 | println!("main receive: {}", val); 33 | 34 | /* multiple sender, one receive */ 35 | println!("*********************** example3 *****************************"); 36 | let tx2 = mpsc::Sender::clone(&tx); 37 | thread::spawn(move || { 38 | let vals = vec![ 39 | String::from("tx2: 1"), 40 | String::from("tx2: 2"), 41 | String::from("tx2: 3"), 42 | ]; 43 | for val in vals { 44 | tx2.send(val).unwrap(); 45 | thread::sleep(Duration::from_secs(1)); 46 | } 47 | }); 48 | 49 | let tx3 = mpsc::Sender::clone(&tx); 50 | thread::spawn(move || { 51 | let vals = vec![ 52 | String::from("tx3: 1"), 53 | String::from("tx3: 2"), 54 | String::from("tx3: 3"), 55 | ]; 56 | for val in vals { 57 | tx3.send(val).unwrap(); 58 | thread::sleep(Duration::from_secs(1)); 59 | } 60 | }); 61 | drop(tx); 62 | 63 | for receive in rx { 64 | println!("main get: {}", receive); 65 | } 66 | 67 | // mutex usage, never foget lock 68 | println!("*********************** example4 *****************************"); 69 | #[derive(Debug)] 70 | struct Test { 71 | x: i32, 72 | y: i32, 73 | } 74 | let m = Mutex::new(Test{x:1, y:2}); 75 | let mut t = m.lock().unwrap(); 76 | t.x = 2; 77 | t.y = 3; 78 | println!("t is {:?}", t); 79 | 80 | //mutex with multiple threads 81 | println!("*********************** example5 *****************************"); 82 | let counter = Arc::new(Mutex::new(0)); 83 | let mut thrs = vec![]; 84 | 85 | for _ in 0..10 { 86 | let thr_ounter = Arc::clone(&counter); 87 | let thr = thread::spawn(move || { 88 | let mut nr = thr_ounter.lock().unwrap(); 89 | *nr = *nr + 1; 90 | }); 91 | thrs.push(thr); 92 | } 93 | 94 | for thr in thrs { 95 | thr.join().unwrap(); 96 | } 97 | println!("Count is {}", counter.lock().unwrap()); 98 | 99 | } 100 | -------------------------------------------------------------------------------- /ownership/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /ownership/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "ownership" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /ownership/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ownership" 3 | version = "0.1.0" 4 | authors = ["fgao"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /ownership/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | // ownership out of scope 3 | println!("*********************** example1 *****************************"); 4 | { 5 | let s = "hello"; 6 | 7 | println!("s is {}", s); 8 | } 9 | //println!("s is {}", s); 10 | 11 | // ownership moved 12 | println!("*********************** example2 *****************************"); 13 | let s1 = String::from("hello"); 14 | let s2 = s1; 15 | //println!("s1 is {}, s2 is {}", s1, s2); 16 | println!("s2 is {}", s2); 17 | 18 | let i1 = 3; 19 | let i2 = i1; 20 | 21 | println!("i1 is {}, i2 is {}", i1, i2); 22 | 23 | // clone 24 | println!("*********************** example3 *****************************"); 25 | let x = 5; 26 | let y = x; 27 | println!("x is {}, y is {}", x, y); 28 | { 29 | let mut s1 = String::from("hello"); 30 | let s2 = s1.clone(); 31 | s1.push_str(", world"); 32 | println!("s1 is {}, s2 is {}", s1, s2); 33 | } 34 | 35 | // move example2 36 | println!("*********************** example4 *****************************"); 37 | let mut s3 = String::from("hello"); 38 | //show_str(s3); 39 | s3 = show_str2(s3); 40 | show_str3(&s3); 41 | println!("s3 is {}", s3); 42 | 43 | // double mut referenc 44 | println!("*********************** example5 *****************************"); 45 | let r1 = &s3; 46 | let r2 = &s3; 47 | //let r3 = &mut s3; 48 | println!("r1 is {}, r2 is {}", r1, r2); 49 | //println!("r3 is {}, s3 is {}", r3, s3); 50 | 51 | // dangle pointer 52 | println!("*********************** example6 *****************************"); 53 | let s4 = no_dangle_ref(); 54 | //let s4 = dangle_ref(); 55 | println!("s4 is {}", s4); 56 | } 57 | 58 | #[allow(dead_code)] 59 | fn show_str(s:String) { 60 | println!("s is {}", s); 61 | } 62 | 63 | #[allow(dead_code)] 64 | fn show_str2(s:String) -> String { 65 | println!("s is {}", s); 66 | s 67 | } 68 | 69 | #[allow(dead_code)] 70 | fn show_str3(s: &String) { 71 | println!("s is {}", s); 72 | } 73 | 74 | /* 75 | fn dangle_ref() -> &String { 76 | let s = String::from("hello"); 77 | &s 78 | } 79 | */ 80 | 81 | fn no_dangle_ref() -> String { 82 | let s = String::from("hello"); 83 | s 84 | } 85 | -------------------------------------------------------------------------------- /rand_nr/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /rand_nr/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "fuchsia-cprng" 5 | version = "0.1.1" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | 8 | [[package]] 9 | name = "libc" 10 | version = "0.2.62" 11 | source = "registry+https://github.com/rust-lang/crates.io-index" 12 | 13 | [[package]] 14 | name = "rand" 15 | version = "0.4.6" 16 | source = "registry+https://github.com/rust-lang/crates.io-index" 17 | dependencies = [ 18 | "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 19 | "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", 20 | "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 21 | "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 22 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 23 | ] 24 | 25 | [[package]] 26 | name = "rand_core" 27 | version = "0.3.1" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | dependencies = [ 30 | "rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 31 | ] 32 | 33 | [[package]] 34 | name = "rand_core" 35 | version = "0.4.2" 36 | source = "registry+https://github.com/rust-lang/crates.io-index" 37 | 38 | [[package]] 39 | name = "rand_nr" 40 | version = "0.1.0" 41 | dependencies = [ 42 | "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", 43 | ] 44 | 45 | [[package]] 46 | name = "rdrand" 47 | version = "0.4.0" 48 | source = "registry+https://github.com/rust-lang/crates.io-index" 49 | dependencies = [ 50 | "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 51 | ] 52 | 53 | [[package]] 54 | name = "winapi" 55 | version = "0.3.8" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | dependencies = [ 58 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 59 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 60 | ] 61 | 62 | [[package]] 63 | name = "winapi-i686-pc-windows-gnu" 64 | version = "0.4.0" 65 | source = "registry+https://github.com/rust-lang/crates.io-index" 66 | 67 | [[package]] 68 | name = "winapi-x86_64-pc-windows-gnu" 69 | version = "0.4.0" 70 | source = "registry+https://github.com/rust-lang/crates.io-index" 71 | 72 | [metadata] 73 | "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" 74 | "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" 75 | "checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" 76 | "checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" 77 | "checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" 78 | "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" 79 | "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" 80 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 81 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 82 | -------------------------------------------------------------------------------- /rand_nr/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rand_nr" 3 | version = "0.1.0" 4 | authors = ["fgao"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | rand = "0.4.*" -------------------------------------------------------------------------------- /rand_nr/src/main.rs: -------------------------------------------------------------------------------- 1 | use rand::Rng; 2 | 3 | fn main() { 4 | let rand_nr = rand::thread_rng().gen_range(1, 100); 5 | 6 | println!("Hello, world! Random number is {}", rand_nr); 7 | } 8 | -------------------------------------------------------------------------------- /safe_mem/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /safe_mem/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "safe_mem" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /safe_mem/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "safe_mem" 3 | version = "0.1.0" 4 | authors = ["fgao"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /safe_mem/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | 3 | // uninitialized 4 | println!("*********************** example1 *****************************"); 5 | let x1: i32 = 1; 6 | let px: &i32 = &x1; 7 | 8 | println!("x1 is {}, px is {}", x1, px); 9 | 10 | // use after free 11 | println!("*********************** example2 *****************************"); 12 | { 13 | let x2:i32 = 2; 14 | //px = &x2; 15 | println!("x2 is {}", x2); 16 | } 17 | println!("px is {}", px); 18 | 19 | // overflow 20 | println!("*********************** example3 *****************************"); 21 | let mut x3:[i32; 3] = [0;3]; 22 | for i in 0..3 { 23 | x3[i] = 3; 24 | } 25 | println!("x3 is {:?}", x3); 26 | 27 | // double free 28 | println!("*********************** example4 *****************************"); 29 | let pt1: Box; 30 | let pt2 = Box::new(12); 31 | 32 | pt1 = pt2; 33 | //println!("pt1 is {}, pt2 is {}", pt1, pt2); 34 | println!("pt1 is {}", pt1); 35 | } 36 | -------------------------------------------------------------------------------- /smart_pointer/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /smart_pointer/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "smart_pointer" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /smart_pointer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "smart_pointer" 3 | version = "0.1.0" 4 | authors = ["fgao"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /smart_pointer/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::rc::Rc; 2 | use std::cell::RefCell; 3 | 4 | fn main() { 5 | #[derive(Debug)] 6 | struct TestStruct1 { 7 | x1: i32, 8 | x2: i32, 9 | }; 10 | 11 | /* Box */ 12 | println!("*********************** example1 *****************************"); 13 | let t1 = Box::new(TestStruct1{x1:1, x2:2}); 14 | println!("t1 is {:?}", t1); 15 | let t2 = t1; 16 | //println!("t1 is {:?}", t1); 17 | println!("t2 is {:?}", t2); 18 | 19 | /* Rc immutable */ 20 | println!("*********************** example2 *****************************"); 21 | let t3 = Rc::new(TestStruct1{x1:1, x2:2}); 22 | println!("t3 is {:?}, strong_cnt is {}", t3, Rc::strong_count(&t3)); 23 | let t4 = t3.clone(); 24 | println!("t3 is {:?}, strong_cnt is {}", t3, Rc::strong_count(&t3)); 25 | println!("t4 is {:?}, strong_cnt is {}", t4, Rc::strong_count(&t4)); 26 | //t3.x1 = 4; 27 | 28 | // Refcell 29 | println!("*********************** example3 *****************************"); 30 | let t5 = Rc::new(RefCell::new(TestStruct1{x1:1, x2:2})); 31 | println!("t5 is {:?}, strong_cnt is {}", t5, Rc::strong_count(&t5)); 32 | let t6 = t5.clone(); 33 | println!("t6 is {:?}, strong_cnt is {}", t6, Rc::strong_count(&t6)); 34 | /* 35 | let t7 = t5.borrow(); 36 | //t7.x1 = 3; 37 | println!("t7 is {:?}, strong_cnt is {}", t7, Rc::strong_count(&t5)); 38 | let t8 = t5.borrow(); 39 | println!("t8 is {:?}", t8); 40 | */ 41 | 42 | 43 | let mut t9 = t5.borrow_mut(); 44 | t9.x1 = 2; 45 | //let t10 = t6.borrow_mut(); 46 | println!("t9 is {:?}", t9); 47 | //println!("t10 is {:?}", t10); 48 | 49 | } 50 | --------------------------------------------------------------------------------