diff options
Diffstat (limited to 'rust/theBook')
32 files changed, 178 insertions, 0 deletions
diff --git a/rust/theBook/chapter-4-understanding-ownership/emp1.rs b/rust/theBook/chapter-4-understanding-ownership/emp1.rs new file mode 100644 index 0000000..a0f4bbd --- /dev/null +++ b/rust/theBook/chapter-4-understanding-ownership/emp1.rs @@ -0,0 +1,9 @@ +fn main() { + let mut s = String::from("hello"); + + let r1 = &s; + let r2 = &s; + println!("{},{}",r1,r2); + let r3 = &mut s; + println!("{r3}"); +} diff --git a/rust/theBook/chapter-4-understanding-ownership/emp2.rs b/rust/theBook/chapter-4-understanding-ownership/emp2.rs new file mode 100644 index 0000000..02a24b0 --- /dev/null +++ b/rust/theBook/chapter-4-understanding-ownership/emp2.rs @@ -0,0 +1,11 @@ +fn main() { + let str = dangle(); + + println!("{str}"); +} + +fn dangle() -> &String { + let s = String::from("Hello"); + + &s +} diff --git a/rust/theBook/chapter-4-understanding-ownership/str.rs b/rust/theBook/chapter-4-understanding-ownership/str.rs new file mode 100644 index 0000000..09bab67 --- /dev/null +++ b/rust/theBook/chapter-4-understanding-ownership/str.rs @@ -0,0 +1,38 @@ +fn first_word(s: &String) -> usize { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return i; + } + } + + s.len() +} + +fn second_word(s: &str) -> &str { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return &s[0..i]; + } + } + + &s[0..s.len()] +} + +fn main() { + let mut s = String::from("hello world"); + + let word = first_word(&s); + + println!("{word}"); + + println!("{}",&s[0..5]); + + let word2 = second_word(&s[..]); + + println!("{}",&word2); + s.clear(); +} diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/emp1.rs b/rust/theBook/chapter-5-using-struct-to-structure-related-data/emp1.rs new file mode 100644 index 0000000..655eef0 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/emp1.rs @@ -0,0 +1,30 @@ +struct User { + active: bool, + sign_in_count: u32, + email: String, + username: String, +} + +fn create_user(email: String, username: String) -> User { + User { + email, + username, + active: true, + sign_in_count: 1, + } +} + +fn main() { + let mail = String::from("example@example.com"); + let user = String::from("name"); + + let user1 = create_user(mail,user); + + println!("{} {} {} {}",user1.email,user1.username,user1.active,user1.sign_in_count); + + let user2 = User { + email: String::from("hello@world.com"), + ..user1 + }; + println!("{} {} {} {}",user2.email,user2.username,user2.active,user2.sign_in_count); +} diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/Cargo.lock b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/Cargo.lock new file mode 100644 index 0000000..22aba19 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "method" +version = "0.1.0" diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/Cargo.toml b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/Cargo.toml new file mode 100644 index 0000000..6c5a978 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "method" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/src/main.rs b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/src/main.rs new file mode 100644 index 0000000..6109e00 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/src/main.rs @@ -0,0 +1,54 @@ +#[derive(Debug)] +struct Rect { + height: u32, + width: u32, +} + +impl Rect { + fn new(size: u32) -> Self { + Self { + height: size, + width: size, + } + } + + fn area(&self) -> u32 { + self.width * self.height + } + + fn can_hold(&self, r2: &Rect) -> bool { + if self.height <= r2.width && self.width <= r2.width { + return false; + } else { + return true; + } + } +} + +fn main() { + let r = Rect { + height: 50, + width: 30, + }; + + println!("The area of the rectangle is {} square pixels.", r.area()); + println!("The r = {:?}",r); + dbg!(&r); // return ownership + + + let r2 = Rect { + height: 40, + width: 10, + }; + + let r3 = Rect { + height: 45, + width: 60, + }; + + println!("Can rect1 hold rect2? {}", r.can_hold(&r2)); + println!("Can rect1 hold rect3? {}", r.can_hold(&r3)); + + let r4 = Rect::new(40); + println!("{:?}", r4); +} diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/.rustc_info.json b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/.rustc_info.json new file mode 100644 index 0000000..9683918 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/.rustc_info.json @@ -0,0 +1 @@ +{"rustc_fingerprint":1612890582466578081,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1)\nbinary: rustc\ncommit-hash: 69f9c33d71c871fc16ac445211281c6e7a340943\ncommit-date: 2022-12-12\nhost: x86_64-unknown-linux-gnu\nrelease: 1.66.0\nLLVM version: 14.0.6\n","stderr":""},"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/usr\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n","stderr":""}},"successes":{}}
\ No newline at end of file diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/CACHEDIR.TAG b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/CACHEDIR.TAG new file mode 100644 index 0000000..20d7c31 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/CACHEDIR.TAG @@ -0,0 +1,3 @@ +Signature: 8a477f597d28d172789f06886806bc55 +# This file is a cache directory tag created by cargo. +# For information about cache directory tags see https://bford.info/cachedir/ diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.cargo-lock b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.cargo-lock new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.cargo-lock diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/bin-method b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/bin-method new file mode 100644 index 0000000..ca86b33 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/bin-method @@ -0,0 +1 @@ +a691507414a49bcb
\ No newline at end of file diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/bin-method.json b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/bin-method.json new file mode 100644 index 0000000..b82a846 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/bin-method.json @@ -0,0 +1 @@ +{"rustc":9725468309624180303,"features":"[]","target":16139928211081419303,"profile":9251013656241001069,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/method-e10ed74834842a40/dep-bin-method"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
\ No newline at end of file diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/dep-bin-method b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/dep-bin-method Binary files differnew file mode 100644 index 0000000..5fdf103 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/dep-bin-method diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/invoked.timestamp b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/invoked.timestamp new file mode 100644 index 0000000..e00328d --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/.fingerprint/method-e10ed74834842a40/invoked.timestamp @@ -0,0 +1 @@ +This file has an mtime of when this was started.
\ No newline at end of file diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/deps/method-e10ed74834842a40 b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/deps/method-e10ed74834842a40 Binary files differnew file mode 100755 index 0000000..bfb8755 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/deps/method-e10ed74834842a40 diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/deps/method-e10ed74834842a40.d b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/deps/method-e10ed74834842a40.d new file mode 100644 index 0000000..1afe682 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/deps/method-e10ed74834842a40.d @@ -0,0 +1,5 @@ +/home/pico/codes/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/deps/method-e10ed74834842a40: src/main.rs + +/home/pico/codes/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/deps/method-e10ed74834842a40.d: src/main.rs + +src/main.rs: diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/1inymij3x0ch8pp9.o b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/1inymij3x0ch8pp9.o Binary files differnew file mode 100644 index 0000000..ffe9181 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/1inymij3x0ch8pp9.o diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/1sqvtoy8vtz4m0un.o b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/1sqvtoy8vtz4m0un.o Binary files differnew file mode 100644 index 0000000..09a1dcc --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/1sqvtoy8vtz4m0un.o diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/28emacl7u7q0r5nb.o b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/28emacl7u7q0r5nb.o Binary files differnew file mode 100644 index 0000000..76fbefc --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/28emacl7u7q0r5nb.o diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/36unv514f6f2demr.o b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/36unv514f6f2demr.o Binary files differnew file mode 100644 index 0000000..9bcceb5 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/36unv514f6f2demr.o diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/3bcq9wv5zzh54w12.o b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/3bcq9wv5zzh54w12.o Binary files differnew file mode 100644 index 0000000..8d9a07c --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/3bcq9wv5zzh54w12.o diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/410pp0jbv822jtw3.o b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/410pp0jbv822jtw3.o Binary files differnew file mode 100644 index 0000000..1ba92fb --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/410pp0jbv822jtw3.o diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/5827txjlbzheowls.o b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/5827txjlbzheowls.o Binary files differnew file mode 100644 index 0000000..4e019e2 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/5827txjlbzheowls.o diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/9pemqmha9q3j8ep.o b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/9pemqmha9q3j8ep.o Binary files differnew file mode 100644 index 0000000..f29e949 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/9pemqmha9q3j8ep.o diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/dep-graph.bin b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/dep-graph.bin Binary files differnew file mode 100644 index 0000000..2117b19 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/dep-graph.bin diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/oa7vpy4no3g983a.o b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/oa7vpy4no3g983a.o Binary files differnew file mode 100644 index 0000000..b01ca06 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/oa7vpy4no3g983a.o diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/query-cache.bin b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/query-cache.bin Binary files differnew file mode 100644 index 0000000..cbb28b3 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/query-cache.bin diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/work-products.bin b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/work-products.bin Binary files differnew file mode 100644 index 0000000..d850296 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv-81g99pd4npj0/work-products.bin diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv.lock b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv.lock new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/incremental/method-wj4k1j5le4zj/s-gglwdrqqt3-1p0l5fv.lock diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/method b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/method Binary files differnew file mode 100755 index 0000000..bfb8755 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/method diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/method.d b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/method.d new file mode 100644 index 0000000..d1f2ab5 --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/method.d @@ -0,0 +1 @@ +/home/pico/codes/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/target/debug/method: /home/pico/codes/rust/theBook/chapter-5-using-struct-to-structure-related-data/method/src/main.rs diff --git a/rust/theBook/chapter-5-using-struct-to-structure-related-data/program_struct/Cargo.toml b/rust/theBook/chapter-5-using-struct-to-structure-related-data/program_struct/Cargo.toml new file mode 100644 index 0000000..5c7f34a --- /dev/null +++ b/rust/theBook/chapter-5-using-struct-to-structure-related-data/program_struct/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "program_struct" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] |