# use std::thread;
# use std::time::Duration;
#
let expensive_closure = |num| {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
num
};
# expensive_closure(5);
闭包的定义
- 以一对
|
开始 - 在|中间指定闭包的参数
- 如果有多个参数, 使用逗号分隔 |param1,param2|
- 闭包不要求像 fn 函数那样在参数和返回值上注明类型
self.containers
.iter()
.enumerate()
.map(|(index,c)| core::Container {
name: c.name.clone(),
image: Some(c.image.clone()),
image_pull_policy: Some("IfNotPresent".to_string()),
resources: Some(c.resources.to_resource_requirements()),
ports: Some(c.ports.iter().map(|p| p.to_container_port()).collect()),
command: c.cmd.clone(),
args: c.args.clone(),
env: Some(
c.env
.iter()
.map(|e| e.to_env_var(resolved_vals.clone()))
.collect(),
),
volume_mounts: c.volume_mounts(index,instance_name.clone()),
liveness_probe: c.liveness_probe.clone().and_then(|p| Some(p.to_probe())),
readiness_probe: c.readiness_probe.clone().and_then(|p| Some(p.to_probe())),
..Default::default()
})
.collect()
网友评论