Skip to content

Instantly share code, notes, and snippets.

@Shaun289
Created November 10, 2021 21:45
Show Gist options
  • Save Shaun289/6dd2d45a3c8d17d008031013690b4e24 to your computer and use it in GitHub Desktop.
Save Shaun289/6dd2d45a3c8d17d008031013690b4e24 to your computer and use it in GitHub Desktop.
Rust study : As input parameters
/*
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