ystd 0.0.11

An opinionated and batteries included `std` mirror for convenient, correct code and pleasant error messages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
use crate::prelude::*;

#[extension(pub trait StringTruncateLossyExt)]
impl String {
	/// Like [String::truncate] but doesn't panic.
	/// 
	/// Somebody please optimize this implementation
	fn truncated_lossy(mut self, new_len: usize) -> String {
		// SAFETY: We then copy basically the whole string confirming its all UTF-8
		unsafe { self.as_mut_vec() }.truncate(new_len);
		String::from_utf8_lossy(self.as_bytes()).into_owned()
	}
}