There are several possible ways to do that. For example, using `vec::from_fn`:

~~~~
// prints `~[1, 2, 3, 4, 5]`
io::println(fmt!("%?", vec::from_fn(5, |x| x+1)))
~~~~

This has a caveat that you should cast to the target type if your
vector is not `~[uint]`. In that case, you can also use `vec::build`
and `<integral_type>::range`:

~~~~
// prints `~[1, 2, 3, 4, 5]`
let v = do vec::build |push| {
    for int::range(1, 6) |i| { push(i); }
};
~~~~

...which can be a bit annoying if you need more of them. Ideally we
may be able to use `vec::range::<int>(1, 6)` which is exactly
equivalent to that, but it is not yet there.


2013/4/15 Alexander Stavonin <[email protected]>:
> I always thought that it is possible to write next code:
>
> let box_box = ~[1 .. 5];
>
> And obtain the vector with [1, 2, 3, 4, 5], but it doesn't works. What is
> the simplest way to do it?
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>



--
-- Kang Seonghoon | Software Engineer, iPlateia Inc. | http://mearie.org/
-- Opinions expressed in this email do not necessarily represent the
views of my employer.
--
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to