How to as_ref a vec containing references?

Because of itertools producing a Vec<&T> when performing partition_map, I need a way of converting the result to &Vec<T> and thought as_ref would be fit for the job, but no sadly not working for me:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5242f6de56c4b19a684a94574d117cad

result:

the trait AsRef<Vec<Box<Ob>>> is not implemented for Vec<&Box<Ob>>

This answer I might be misreading that maybe this sort of conversion isn't supported by as_ref? If not, what's the simplest way to go about it?

To have &Vec<T>, you must somewhere have Vec<T> itself - reference can't point to nowhere. What's the map_partition you're referring to? I'm not able to find it in the docs.

Ah, from SO:

someVec.cloned().collect()

Note that this will clone every T in the vector - this might be expensive and/or undesirable, depending on your task and the T in question.

yes, you're right I don't like the solution I found either for that reason.

Here is the "partitions_map" (sorry, dyslexia) I'm using:
https://docs.rs/itertools/0.10.1/itertools/trait.Itertools.html#method.partition_map

Could you share the code yielding Vec<&V>? Looking at the docs, it seems that partition_map will output containers holding the same type you've returned from the passed closure, so either this closure or the incoming iterator might be giving you something you're not expecting.

Good call - I've updated link in my question to include my use of itertools

Couldn't you just replace ob.iter() with ob.into_iter()? This should yield the owned values for the whole pipeline.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.