Loading RustViz Engine...
Truncate a string slice without panicking on multi-byte UTF-8 boundaries.
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
pub fn safe_truncate(s: &str, max_bytes: usize) -> &str {
if max_bytes >= s.len() { return s; }
let mut end = max_bytes;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}