Ok so I figured that my last “solution” to this problem was pretty useless. Adding as_mut_slice doesn’t really make sense.
@mzabaluev: I agree that from_raw_parts is not the safest thing to use. But I don’t think a RAII guard like the one you proposed does really make sense. Your code still relies on several assumptions about the internal implementation of String, which isn’t optimal. And again: I don’t think that it’s really useful to edit the string buffer in an alternating UTF8-safe and UTF8-unsafe way.
And whenever we want to work on the string buffer without UTF8 checks, we could do:
let mut v = s.into_bytes();
// do stuff with v
s = String::from_utf8_unchecked(v);
And this is not really more work than as_mut_vec, right?
So my new proposal is: Just remove as_mut_vec. You can do everything it could do with other methods anyway.
I don’t really like the whole std::string design anyway… But removing the method as_mut_vec is a step in the right direction, I think.