How to sort immutable slice in Rust? - rust

Suppose that I have the following code snippet:
fn foo(xs: &[i32]) {
xs.sort(); // doesn't work since not mut
// computation on xs
}
This function takes in an unsorted immutable slice but I need it to be sorted for computations later on in the function. I also cannot change the function signature. Is there a way to sort xs out of place or a way to convert xs into a mutable slice?
I've tried cloning and mapping but it seems that the problem is with the type of xs.

Since the data is immutable, you have to make a mutable copy of it first. You do not statically know the size of the slice parameter, so you need to use a Vec (instead of an array) to copy into. Then you can sort that Vec and use it normally in the rest of your function:
fn foo(xs: &[i32]) {
let mut xs = xs.to_vec();
xs.sort();
// computation on xs
}

Related

How to return a Vec<String> from a collection in rust?

here I am splitting vec into sub-vectors of equal size 4 and then returning a collection, I want the type returned from the collection to be Vec<String>. How can I do so?
let mut split_operations : Vec<String> = vec[2..].chunks(4).collect::<String>();
This generates the following error:
a value of type std::string::String cannot be built from an iterator over elements of type &[std::string::String]
value of type std::string::String cannot be built from std::iter::Iterator<Item=&[std::string::String]>
help: the trait std::iter::FromIterator<&[std::string::String]> is not implemented for std::string::Stringrustc(E0277)
The type parameter of collect() is what the entire thing should be collected to. So the parameter to collect and the actual type are redundant, only one is needed.
However Rust doesn't want to guess how or what to the chunks are supposed to change: here you have an iterator of slices (Iterator<Item=&[String]). You can collect an Iterator<Item=String> to a Vec<String> or to a single String (it'll join all the items), but the intermediate slice means Rust's got no idea what's supposed to happen.
If you want each chunk to become a single string, you need to do that explicitely in a map e.g.
let mut split_operations : Vec<String> = vec[2..].chunks(4)
.map(|c| c.concat())
.collect();
If you want to play (and / or infuriate your colleagues), you can also write the map callback as <[_]>::concat.

rust zip function that copies values rather than referencing them

I would like to zip two vectors together, but what I get when calling the zip function is (&i32, &i32). I would like to get (i32, i32) - copy values from both vectors into a new vector.
let v1 = vec![1,2,3];
let v1 = vec![4,5,6];
// what I want
let zipped : Vec<(i32, i32)> = v1.iter().zip(v2.iter()).collect();
// what I actually get
let zipped : Vec<(&i32, &i32)> = v1.iter().zip(v2.iter()).collect();
Is it possible to force the zip function to copy the values?
zip() doesn't influence the values you're iterating over, it simply creates an iterator over tuples of the first and second iterator's values.
If you want to get owned values, you can use into_iter() on the Vecs. This will consume the vectors, so you can't use them anymore after the call. If you need to keep those vectors around, there's a copied() method that can be called on iterators over types that implement Copy, which is the case for i32. So you can get the same result while keeping the Vecs around by v1.iter().copied().zip(v2.iter().copied()).collect().
You can use cloned:
let zipped : Vec<(i32, i32)> = v1.iter().cloned().zip(v2.iter().cloned()).collect();
Playground

What is the difference between TryFrom<&[T]> and TryFrom<Vec<T>>?

There seem to be two ways to try to turn a vector into an array, either via a slice (fn a) or directly (fn b):
use std::array::TryFromSliceError;
use std::convert::TryInto;
type Input = Vec<u8>;
type Output = [u8; 1000];
// Rust 1.47
pub fn a(vec: Input) -> Result<Output, TryFromSliceError> {
vec.as_slice().try_into()
}
// Rust 1.48
pub fn b(vec: Input) -> Result<Output, Input> {
vec.try_into()
}
Practically speaking, what's the difference between these? Is it just the error type? The fact that the latter was added makes me wonder whether there's more to it than that.
They have slightly different behavior.
The slice to array implementation will copy the elements from the slice. It has to copy instead of move because the slice doesn't own the elements.
The Vec to array implementation will consume the Vec and move its contents to the new array. It can do this because it does own the elements.

Immutable access in rust

I am new to rust from python and have used the functional style in python extensively.
What I am trying to do is to take in a string (slice) (or any iterable) and iterate with a reference to the current index and the next index. Here is my attempt:
fn main() {
// intentionally immutable, this should not change
let x = "this is a
multiline string
with more
then 3 lines.";
// initialize multiple (mutable) iterators over the slice
let mut lineiter = x.chars();
let mut afteriter = x.chars();
// to have some reason to do this
afteriter.skip(1);
// zip them together, comparing the current line with the next line
let mut zipped = lineiter.zip(afteriter);
for (char1, char2) in zipped {
println!("{:?} {:?}", char1, char2);
}
}
I think it should be possible to get different iterators that have different positions in the slice but are referring to the same parts of memory without having to copy the string, but the error I get is as follows:
error[E0382]: use of moved value: `afteriter`
--> /home/alex/Documents/projects/simple-game-solver/src/src.rs:15:35
|
10 | let afteriter = x.chars();
| --------- move occurs because `afteriter` has type `std::str::Chars<'_>`, which does not implement the `Copy` trait
11 | // to have some reason to do this
12 | afteriter.skip(1);
| --------- value moved here
...
15 | let mut zipped = lineiter.zip(afteriter);
| ^^^^^^^^^ value used here after move
I also get a warning telling me that zipped does not need to be mutable.
Is it possible to instantiate multiple iterators over a single variable and if so how can it be done?
Is it possible to instantiate multiple iterators over a single variable and if so how can it be done?
If you check the signature and documentation for Iterator::skip:
fn skip(self, n: usize) -> Skip<Self>
Creates an iterator that skips the first n elements.
After they have been consumed, the rest of the elements are yielded. Rather than overriding this method directly, instead override the nth method.
You can see that it takes self by value (consumes the input iterator) and returns a new iterator. This is not a method which consumes the first n elements of the iterator in-place, it's one which converts the existing iterator into one which skips the first n elements.
So instead of:
let mut afteriter = x.chars();
afteriter.skip(1);
you just write:
let mut afteriter = x.chars().skip(1);
I also get a warning telling me that zipped does not need to be mutable.
That's because Rust for loop uses the IntoIterator trait, which moves the iterable into the loop. It's not creating a mutable reference, it's just consuming whatever the RHS is.
Therefore it doesn't care what the mutability of the variable. You do need mut if you iterate explicitly, or if you call some other "terminal" method (e.g. nth or try_fold or all), or if you want to iterate on the mutable reference (that's mostly useful for collections though), but not to hand off iterators to some other combinator method, or to a for loop.
A for loop takes self, if you will. Just as for_each does in fact.
Thanks to #Stargateur for giving me the solution. The .skip(1) takes ownership of afteriter and returns ownership to a version without the first element. What was happening before was ownership was lost on the .skip and so the variable could not be mutated anymore (I am pretty sure)

How do I convert a Vec<String> to Vec<&str>?

I can convert Vec<String> to Vec<&str> this way:
let mut items = Vec::<&str>::new();
for item in &another_items {
items.push(item);
}
Are there better alternatives?
There are quite a few ways to do it, some have disadvantages, others simply are more readable to some people.
This dereferences s (which is of type &String) to a String "right hand side reference", which is then dereferenced through the Deref trait to a str "right hand side reference" and then turned back into a &str. This is something that is very commonly seen in the compiler, and I therefor consider it idiomatic.
let v2: Vec<&str> = v.iter().map(|s| &**s).collect();
Here the deref function of the Deref trait is passed to the map function. It's pretty neat but requires useing the trait or giving the full path.
let v3: Vec<&str> = v.iter().map(std::ops::Deref::deref).collect();
This uses coercion syntax.
let v4: Vec<&str> = v.iter().map(|s| s as &str).collect();
This takes a RangeFull slice of the String (just a slice into the entire String) and takes a reference to it. It's ugly in my opinion.
let v5: Vec<&str> = v.iter().map(|s| &s[..]).collect();
This is uses coercions to convert a &String into a &str. Can also be replaced by a s: &str expression in the future.
let v6: Vec<&str> = v.iter().map(|s| { let s: &str = s; s }).collect();
The following (thanks #huon-dbaupp) uses the AsRef trait, which solely exists to map from owned types to their respective borrowed type. There's two ways to use it, and again, prettiness of either version is entirely subjective.
let v7: Vec<&str> = v.iter().map(|s| s.as_ref()).collect();
and
let v8: Vec<&str> = v.iter().map(AsRef::as_ref).collect();
My bottom line is use the v8 solution since it most explicitly expresses what you want.
The other answers simply work. I just want to point out that if you are trying to convert the Vec<String> into a Vec<&str> only to pass it to a function taking Vec<&str> as argument, consider revising the function signature as:
fn my_func<T: AsRef<str>>(list: &[T]) { ... }
instead of:
fn my_func(list: &Vec<&str>) { ... }
As pointed out by this question: Function taking both owned and non-owned string collections. In this way both vectors simply work without the need of conversions.
All of the answers idiomatically use iterators and collecting instead of a loop, but do not explain why this is better.
In your loop, you first create an empty vector and then push into it. Rust makes no guarantees about the strategy it uses for growing factors, but I believe the current strategy is that whenever the capacity is exceeded, the vector capacity is doubled. If the original vector had a length of 20, that would be one allocation, and 5 reallocations.
Iterating from a vector produces an iterator that has a "size hint". In this case, the iterator implements ExactSizeIterator so it knows exactly how many elements it will return. map retains this and collect takes advantage of this by allocating enough space in one go for an ExactSizeIterator.
You can also manually do this with:
let mut items = Vec::<&str>::with_capacity(another_items.len());
for item in &another_items {
items.push(item);
}
Heap allocations and reallocations are probably the most expensive part of this entire thing by far; far more expensive than taking references or writing or pushing to a vector when no new heap allocation is involved. It wouldn't surprise me if pushing a thousand elements onto a vector allocated for that length in one go were faster than pushing 5 elements that required 2 reallocations and one allocation in the process.
Another unsung advantage is that using the methods with collect do not store in a mutable variable which one should not use if it's unneeded.
another_items.iter().map(|item| item.deref()).collect::<Vec<&str>>()
To use deref() you must add using use std::ops::Deref
This one uses collect:
let strs: Vec<&str> = another_items.iter().map(|s| s as &str).collect();
Here is another option:
use std::iter::FromIterator;
let v = Vec::from_iter(v.iter().map(String::as_str));
Note that String::as_str is stable since Rust 1.7.

Resources