Trampled by a Cow

I have an iterator whose Items are Cow<'static, str>, and I'd love to convert that into a Vec<&'static str>. After searching through docs and experimenting for quite some time, I must admit defeat to my bovine adversary.

Just how does an honest farmer programmer borrow the contents of a Cow?

1 Like

Realize that an owned string (String) can be stored in a Cow<'static, str>. An owned string is not &'static str, and the only way to do so would be explicitly leaking memory. Needless to say I do not recommend this.

3 Likes

You could get a Vec<'someshorterlifetime str> with iterator.map(|cow| cow.as_ref()).collect(), if the shorter lifetime fits your needs. (I see in the other thread you're battling some questionable APIs.)

1 Like

Thanks guys, in the end I'm going to try and be less fancy and just yield Vec<String> after doing some more intermediate manipulations I didn't describe earlier. The Vec is short anyway, the allocations won't be the end of the world here.

Sometimes you cannulate the Cow, and sometimes the Cow cannulates you.

3 Likes

Rather offtopic, but for that to be a natural thought to you I wonder what interesting hobbies you must have :thinking:

Thanks for teaching me a new verb.

1 Like

cow.into_owned() can unify all items to String, but not the other way around.

1 Like

This was the key insight which was new information to me.

1 Like

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.