after applying a few combinators to a slice of objects &[T] I end up with a slice of references &[&T] which I like to process in another function. So the function parameter must be of type &[&T]. In order to reuse this function, I have to convert slice of objects to slices of references which is cumbersome and feels wrong. I'm wondering, if I'm missing something here which would allow me to resolve this in a more elegant way.
In this example, the total sum of all ages from a list of Option<Age>s should be computed. The function of interest is sum_ages that takes a slice of type &[&Age] instead a of slice of &[Age] which is more general and more applicable.
Do you have any idea how to ease the pain and make sum_ages reusable without requiring a transformation of &[&Age] to &[&Age] all the time?
This works because you are effectively "abstracting" over whether you have something or a reference to it. In this case, as long as you can get a reference to the thing then that's all we care about.
I was already pondering about using AsRef but it strikes me as odd that I need to set a property of the object in order to ease on how I pass it to functions. I will follow this path nevertheless.
I have a question about your vector. You are storing Option in the vector. Is this something that's okay to do? It seems like you'd have to go through extra work to make sure that option contains something later on, rather than just storing Age in the vector.
Option and its sibling Result are types that encapsulate the result of a computation that might have not produced a valid result or even failed doing so, respectively. So if you apply such a computation to a set of input objects you end up with a list of Options or Results that are used in a following step of your algorithm.
In my actual case, I get a vector of Results from the first step of my algorithm. The second step of my algorithm is only interested in successful results of step 1. So I need to filter / partition the results before I hand them over to the second step. But it has been easier to make up a minimized example using Option.
I believe AsRef and Borrow meant for two slightly different things. AsRef abstracts over whether you have a reference or own the resource, whereas Borrow abstracts over whether you have a mutable or immutable reference.