Created
November 9, 2021 04:23
-
-
Save Shaun289/ff5808c2e3f85bbc9202187c190abe5a to your computer and use it in GitHub Desktop.
Rust study : closure capturing
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/capture.html | |
compiled on https://play.rust-lang.org/ | |
result : | |
*/ | |
fn main() | |
{ | |
use std::mem; | |
let color = "green"; | |
let print = || println!("color: {}", color); | |
print(); | |
print(); | |
let mut count = 0; | |
let mut inc = || { | |
count += 1; | |
println!("count : {}", count); | |
}; | |
inc(); | |
inc(); | |
// 복사가 아닌 타입 | |
let movable = Box::new(3); | |
let consume = || { | |
println!("movable : {:?}", movable); | |
mem::drop(movable); | |
}; | |
consume(); | |
// 컴파일 에러 | |
//consume(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment