Multiple distinct objects: Difference between revisions
Content added Content deleted
(Added Algol W) |
(Added Rust) |
||
Line 1,178: | Line 1,178: | ||
<lang ruby>Array.new(n) { Foo.new }</lang> |
<lang ruby>Array.new(n) { Foo.new }</lang> |
||
which evaluates <code>Foo.new</code> <var>n</var> times and collects each result in an Array. This last form is also discussed [[Two-dimensional array (runtime)#Ruby|here]], on the correct construction of a two dimensional array. |
which evaluates <code>Foo.new</code> <var>n</var> times and collects each result in an Array. This last form is also discussed [[Two-dimensional array (runtime)#Ruby|here]], on the correct construction of a two dimensional array. |
||
=={{header|Rust}}== |
|||
<lang Rust>use std::rc::Rc; |
|||
use std::cell::RefCell; |
|||
fn main() { |
|||
let size = 3; |
|||
// Clone the given element to fill out the vector. |
|||
let mut v: Vec<String> = vec![String::new(); size]; |
|||
v[0].push('a'); |
|||
println!("{:?}", v); |
|||
// Run a given closure to create each element. |
|||
let mut v: Vec<String> = (0..size).map(|i| i.to_string()).collect(); |
|||
v[0].push('a'); |
|||
println!("{:?}", v); |
|||
// For multiple mutable views of the same thing, use something like Rc and RefCell. |
|||
let v: Vec<Rc<RefCell<String>>> = vec![Rc::new(RefCell::new(String::new())); size]; |
|||
v[0].borrow_mut().push('a'); |
|||
println!("{:?}", v); |
|||
}</lang> |
|||
{{out}} |
|||
<pre>["a", "", ""] |
|||
["0a", "1", "2"] |
|||
[RefCell { value: "a" }, RefCell { value: "a" }, RefCell { value: "a" }]</pre> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |