Hacker News new | ask | show | jobs
by singingboyo 3028 days ago
> Deciding betweene two distinct types &str and &string (not mut &string) for your function's interface is nonsense.

There's really no decision to be made.If you don't want to mutate the argument, use &str, you can still call the function with an &String. If you need mutation, take ownership with String or a mutable ref with &mut String.

> Additionally, that dereferencing a string returns a pointer...

You can't deref a String, you can only deref a reference (&String), not an object. &String derefs to &str. String.str_method() where str_method takes &str works because it'll auto-ref String -> &String, and then deref to &str.

IMO you're making a mountain out of a molehill, it makes a lot of sense once you use it for any time at all.

1 comments

> You can't deref a String

You can absolutely deref a String. Not necessarily usefully (or to the satisfaction of the compiler) as it yields an unsized `str` (exactly the same as deref'ing an &str) but you can certainly do it.

    let a = "foo".to_owned();
    let b = &*a;
works perfectly fine.

Incidentally you can also deref' a Vec, that yields a slice (actual sequence, not the commonly seen &[])