Jump to content

Closures/Value capture: Difference between revisions

(fixed scheme code to task accordance)
Line 1,166:
 
=={{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: ~[proc() -Vec<_> uint] = range(0u,0..10).map(|i| {proc()move || i*i} ).collect();
println!("7th val: {}", fs[7]());
}</lang>
 
{{out}}
$ ./rosetta
'''<pre>7th val: 49'''</pre>
 
'''7th val: 49'''
 
=={{header|Scheme}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.