| Crates.io | unicode-truncate |
| lib.rs | unicode-truncate |
| version | 2.0.1 |
| created_at | 2019-08-15 02:50:03.550433+00 |
| updated_at | 2026-01-15 23:49:26.204889+00 |
| description | Unicode-aware algorithm to pad or truncate `str` in terms of displayed width. |
| homepage | https://github.com/Aetf/unicode-truncate |
| repository | https://github.com/Aetf/unicode-truncate |
| max_upload_size | |
| id | 156940 |
| size | 64,871 |
Unicode-aware algorithm to pad or truncate str in terms of displayed width.
Safely truncate string to display width even not at character boundaries.
use unicode_truncate::UnicodeTruncateStr;
fn main() {
assert_eq!("你好吗".unicode_truncate(5), ("你好", 4));
}
Making sure the string is displayed in exactly number of columns by combining padding and truncating.
use unicode_truncate::UnicodeTruncateStr;
use unicode_truncate::Alignment;
use unicode_width::UnicodeWidthStr;
fn main() {
let str = "你好吗".unicode_pad(5, Alignment::Left, true);
assert_eq!(str, "你好 ");
assert_eq!(str.width(), 5);
}
unicode-truncate can be built without std by disabling the default feature std. However, in
that case unicode_truncate::UnicodeTruncateStr::unicode_pad won't be available because it depends
on std::string::String and std::borrow::Cow.
The Minimum Supported Rust Version (MSRV) is specified in the rust-version field of Cargo.toml.
This version requirement applies only to using the library. Development workflows (such as running benchmarks) require a more recent Rust toolchain, primarily to satisfy the stricter MSRV of dev-dependencies like criterion.