Vector from a range with negative bounds

How can I create a vector with values from -1 to -10 or -10 to -1? And if I already have a vector, how can I fill it with consecutive values (like "iota" in other langs)? Is there an easier way to access the last element other than v[v.len() - 1]?

You could do

let mut v: Vec<i32> = (-10..0).collect();

If you want the last element you could get it with v.pop() or v.last()

Or if you want to add to the vector you can do v.push(223).

Thanks! So it's "last". Is there something like "end" in Julia or "$" in D? How about a Vector from -1 to -10?

Since your bounds for a range (the fancy .. syntax) must be ordered such that in x..y x ≤ y for most functions to work properly, you have to do

let mut v: Vec<i32> = (-10..-1).rev().collect();

This will reverse it to then give you [-1, -10].

What else would you like the last function to do? It returns Option<&T>, while pop returns Option<T>.

Thanks! What about the "iota" -like function, filling a vector v with consecutive values? And is there an "isqrt" function(the result is an integer)? Sorry for the many questions..

What else would you like the last function to do? It returns Option<&T> , while pop returns Option<T>.

The "end" in Julia can be used like v[end] (last element), v[end - 1](second to last) and v[div(end, 2)]. "last" is what I was looking for but if there's something like Julia's "end" then it's a big bonus.

For D, it seems to me that $ is used in slicing arrays to refer to the last element inclusively, which in rust is just an unbounded range on the right hand side:

let v = /**/;
let v_all_but_first_2 = &v[2..];

As for the iota function, do you mean that it extends or overwrites? You can extend with an iterator like so:

let mut v = /* initial vec */;

Isn't end therefore just the length of the vector minus one? If that's the case then we don't have that, and it would be a bit cumbersome to define since it'd have to return an Option<usize>, since underflowing a number is not supported (in the case where len is 0, endis-1, but that's not representable in a usize`).

Thanks for the clarification about "end"!. I meant overwriting the elements of a vector, filling it with consecutive values.

let n = v.len();
v[n-1] = -20;

I'm not too sure about this one; there's probably someone on here who can come up with an elegant solution using adaptors, etc.
Note that if you have a slice which already has the values, you can use [T]::copy_from_slice since Vec<T>: DerefMut<Target = [T]> and Vec<T>: IndexMut<impl RangeBounds, Output = [T]>.

If you just want to replace the elements of a vector with consecutive integers, I would do this:


Note that you can use a ..= b if you wish b to be included in the range, and you can use (a..b).rev() to reverse the ordering.

If you wish to only replace the end of the vector, simply truncate it followed by extend. If you wish to replace something in the middle, you can use the splice function in which the replace_with argument can be a range such as a..b or a reversed range (a..b).rev()

I was thinking about just specifying an initial value and not copying a slice but "copy_from_slice" is a great bonus...
How do I read and write a bunch of integers(per line) from stdin to stdout by using buffered i/o and holding locks? Please provide a slimple but complete(including use) example...

The Stdin type has a read_line function, which you can combine with split_ascii_whitespace. If you have more questions on doing I/O, I recommend you start a new thread (optionally linking to this one).

Oh okay..thanks. I learned new things..

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.