Closures/Value capture: Difference between revisions

Content added Content deleted
(fixed scheme code to task accordance)
Line 1,166: Line 1,166:


=={{header|Rust}}==
=={{header|Rust}}==
One note here about referencing values and capturing values: <br>
<lang rust>fn main() {
Rust employs strong ownership rules that do not allow mutating a value that is referenced (pointed to without allowing mutation) from elsewhere. It also doesn't allow referencing a value that may be dropped before the reference is released. The proof that we really did capture the value is therefore unnecessary. Either we did or it wouldn't have compiled.
let fs: ~[proc() -> uint] = range(0u,10).map(|i| {proc() i*i}).collect();
println!("7th val: {}", fs[7]());
}
</lang>


<lang rust>fn main() {
$ rustc rosetta.rs
let fs: Vec<_> = (0..10).map(|i| {move || i*i} ).collect();
println!("7th val: {}", fs[7]());
}</lang>


{{out}}
$ ./rosetta
<pre>7th val: 49</pre>

'''7th val: 49'''


=={{header|Scheme}}==
=={{header|Scheme}}==