1

I am looking for an equivalent version of this function, either from std or from external crate.

fn add<T>(v: Vec<T>, item: T) -> Vec<T> {
    v.push(item);
    v
}

The purpose of this function is to use it with some reducer (in my case, I need it for itertool's fold_result).

  • 1
    Requests for external libraries are off-topic on SO, and I don't know of one that provides such a function, anyway. However, if you start with an empty `Vec`, passing this function to `fold_results` is the same as `collect`ing into a `Result, E>`. (Unfortunately there's no one-liner equivalent if the `Vec` doesn't start empty.) – trent Mar 21 '20 at 02:54
  • 3
    Why does this function have to be in std or an external crate, when you can just use the code in your question? – Aloso Mar 21 '20 at 03:56
  • 1
    This sounds wrong to me. If you want to append one item to a vector for each item processed, that's a `map()`, not a `fold()`. If you want to stop on the first error, you should collect your iterator of `Result` into a `Result, E>` which will be equivalent to using `fold_result()`. – Sven Marnach Mar 21 '20 at 09:26
  • 2
    We can provide better guidance if you describe your actual use case, i.e. somehting like "this is my iterator, this is the desired result, here is what I tried, but I don't like this aspect of my solution" etc. – Sven Marnach Mar 21 '20 at 09:28

0 Answers0