update at 2020-06-16 18:11:50 by ehlxr
This commit is contained in:
commit
4c9a6dec3c
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/target
|
||||||
|
**/*.rs.bk
|
||||||
|
.vscode
|
||||||
|
.idea
|
58
Cargo.lock
generated
Normal file
58
Cargo.lock
generated
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "hello_macro"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hello_macro_derive"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "0.4.30"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "0.6.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rust_study"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"hello_macro 0.1.0",
|
||||||
|
"hello_macro_derive 0.1.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "0.14.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-xid"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[metadata]
|
||||||
|
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
|
||||||
|
"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
|
||||||
|
"checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741"
|
||||||
|
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
|
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "rust_study"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["ehlxr <ehlxr.me@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
hello_macro = { path = "../hello_macro" }
|
||||||
|
hello_macro_derive = { path = "../hello_macro/hello_macro_derive" }
|
1488
src/main.rs
Normal file
1488
src/main.rs
Normal file
File diff suppressed because it is too large
Load Diff
21
src/ownership_1.rs
Normal file
21
src/ownership_1.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
fn main() {
|
||||||
|
|
||||||
|
let s = String::from("hello"); // s 进入作用域
|
||||||
|
|
||||||
|
takes_ownership(s);// s 的值移动到函数里 ...
|
||||||
|
|
||||||
|
// ... 所以到这里不再有效
|
||||||
|
|
||||||
|
let x = 5;// x 进入作用域
|
||||||
|
|
||||||
|
makes_copy(x);// x 应该移动函数里,
|
||||||
|
// 但 i32 是 Copy 的,所以在后面可继续使用 x
|
||||||
|
} // 这里, x 先移出了作用域,然后是 s。但因为 s 的值已被移走,所以不会有特殊操作
|
||||||
|
|
||||||
|
fn takes_ownership(some_string: String) { // some_string 进入作用域
|
||||||
|
println!("{}", some_string);
|
||||||
|
} // 这里,some_string 移出作用域并调用 `drop` 方法。占用的内存被释放
|
||||||
|
|
||||||
|
fn makes_copy(some_integer: i32) { // some_integer 进入作用域
|
||||||
|
println!("{}", some_integer);
|
||||||
|
} // 这里,some_integer 移出作用域。不会有特殊操作
|
21
src/ownership_2.rs
Normal file
21
src/ownership_2.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
fn main() {
|
||||||
|
let s1 = gives_ownership(); // gives_ownership 将返回值 移给 s1
|
||||||
|
println!("{}", s1);
|
||||||
|
|
||||||
|
let s2 = String::from("hello"); // s2 进入作用域
|
||||||
|
// println!("{}", s2);
|
||||||
|
|
||||||
|
let s3 = takes_and_gives_back(s2); // s2 被移动到 takes_and_gives_back 中, 它也将返回值移给 s3
|
||||||
|
println!("{}", s3);
|
||||||
|
} // 这里, s3 移出作用域并被丢弃。s2 也移出作用域,但已被移走,所以什么也不会发生。s1 移出作用域并被丢弃
|
||||||
|
|
||||||
|
fn gives_ownership() -> String { // gives_ownership 将返回值移动给调用它的函数
|
||||||
|
let some_string = String::from("hello"); // some_string 进入作用域.
|
||||||
|
some_string // 返回 some_string 并移出给调用的函数
|
||||||
|
}
|
||||||
|
|
||||||
|
// takes_and_gives_back 将传入字符串并返回该值
|
||||||
|
fn takes_and_gives_back(a_string: String) -> String { // a_string 进入作用域
|
||||||
|
a_string
|
||||||
|
// 返回 a_string 并移出给调用的函数
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user