I want to write a function which takes a mutable string and checks if the first and last character are the "
character. If so, those two characters should be replaced with the backtick character `
. I've come up with this solution:
fn replace_wrapping_char(s: &mut String) {
if s.len() > 1 && s.starts_with('"') && s.ends_with('"') {
unsafe {
let v = s.as_mut_vec();
v[0] = '`' as u8;
*v.last_mut().unwrap() = '`' as u8;
}
}
}
This seems to work (yes, '`'.is_ascii()
returns true), but it uses unsafe
and looks a bit ugly to me.
Is there a safe and concise way to achieve what I want?