Why does split_at for type String return (&str, &str)


#1

Hi,

why does the split_at function for type String return (&str, &str) ?

fn split_at(&self, mid: usize) -> (&str, &str)

Why doesn’t it return (String, String)?

What is the idiomatic why to turn them into (String, String)?

Best,

Markus


#2

There’s no reason to allocate more memory, as you don’t always need to.

You can call .to_string() on those &str s to get Strings out of them. If it returned Strings directly, you wouldn’t have the option.


#3

Thanks Steve, as always a very useful explanation. I was confused by some type mismatches but I am closer to figuring it out :slight_smile: