-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7505665
commit 70ccf33
Showing
19 changed files
with
258 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
fn main() { | ||
let mut name = String::from("hello"); | ||
let mut name1 = String::from("hola"); | ||
|
||
// 捕获 &mut name | ||
let mut c = || { | ||
name.push_str(" Danny"); | ||
println!("c: {}", name); | ||
}; | ||
|
||
// 捕获 mut name1,注意 name1 需要声明成 mut | ||
let mut c1 = move || { | ||
name1.push_str("!"); | ||
println!("c1: {}", name1); | ||
}; | ||
|
||
c(); | ||
c1(); | ||
|
||
call_mut(&mut c); | ||
call_mut(&mut c1); | ||
|
||
call_once(c); | ||
call_once(c1); | ||
} | ||
|
||
// 在作为参数时,FnMut 也要显式地使用 mut,或者 &mut | ||
fn call_mut(c: &mut impl FnMut()) { | ||
c(); | ||
} | ||
|
||
// 想想看,为啥 call_once 不需要 mut? | ||
fn call_once(c: impl FnOnce()) { | ||
c(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
fn main() { | ||
let v = vec![0u8; 1024]; | ||
let v1 = vec![0u8; 1023]; | ||
|
||
// Fn,不移动所有权 | ||
let mut c = |x: u64| v.len() as u64 * x; | ||
// Fn,移动所有权 | ||
let mut c1 = move |x: u64| v1.len() as u64 * x; | ||
|
||
println!("direct call: {}", c(2)); | ||
println!("direct call: {}", c1(2)); | ||
|
||
println!("call: {}", call(3, &c)); | ||
println!("call: {}", call(3, &c1)); | ||
|
||
println!("call_mut: {}", call_mut(4, &mut c)); | ||
println!("call_mut: {}", call_mut(4, &mut c1)); | ||
|
||
println!("call_once: {}", call_once(5, c)); | ||
println!("call_once: {}", call_once(5, c1)); | ||
} | ||
|
||
fn call(arg: u64, c: &impl Fn(u64) -> u64) -> u64 { | ||
c(arg) | ||
} | ||
|
||
fn call_mut(arg: u64, c: &mut impl FnMut(u64) -> u64) -> u64 { | ||
c(arg) | ||
} | ||
|
||
fn call_once(arg: u64, c: impl FnOnce(u64) -> u64) -> u64 { | ||
c(arg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "chapter12-unsafe" | ||
version.workspace = true | ||
edition.workspace = true | ||
authors.workspace = true | ||
|
||
|
||
[[bin]] | ||
name = "unsafe1" | ||
path = "src/unsafe1-caller.rs" | ||
|
Oops, something went wrong.