Created
November 10, 2021 21:45
-
-
Save Shaun289/6dd2d45a3c8d17d008031013690b4e24 to your computer and use it in GitHub Desktop.
Rust study : As input parameters
This file contains hidden or 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
/* | |
The rust by example ko https://hanbum.gitbooks.io/rustbyexample/content/fn/closures/input_parameters.html | |
compiled on https://play.rust-lang.org/ | |
Fn 클로저는 참조로 획득 (&T) | |
FnMut 클로저는 가변참조로 획득 (&mut T) | |
FnOnce 클로저는 값으로 획득 (T) | |
... | |
이해가 잘 안되네.. | |
*/ | |
fn apply<F>(f: F) where F: FnOnce() { | |
f() | |
} | |
fn apply_to_3<F>(f: F) -> i32 where | |
F: Fn(i32) -> i32 { | |
f(3) | |
} | |
fn main() { | |
use std::mem; | |
let greeting = String::from("hello"); | |
let mut farewell = String::from("goodbye"); | |
let diary = || { | |
println!("I said {} ", greeting); | |
farewell.push_str("!!!"); | |
println!("Then I screamed {}", farewell); | |
//mem::drop(farewell); | |
}; | |
apply(diary); | |
//apply(diary); | |
let double = |x| 2 * x; | |
println!("3 double = {}", apply_to_3(double)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment