#slice #string #mutability

无std mut-str

用于处理可变字符串切片(&mut str)的工具包

8个版本 (3个稳定版)

1.1.0-alpha.22024年7月27日
1.1.0-alpha.12024年5月7日
1.0.1 2023年10月16日
1.0.0-alpha.32023年9月28日

#209 in 文本处理

Download history 221/week @ 2024-05-03 22/week @ 2024-05-10 11/week @ 2024-05-17 4/week @ 2024-05-24 11/week @ 2024-07-05 5/week @ 2024-07-19 137/week @ 2024-07-26 8/week @ 2024-08-02

150 每月下载量

MIT/Apache

120KB
2K SLoC

mut-str

一个用于处理可变字符串切片(&mut str)和不可变字符串切片的工具包!

几乎在标准库中你可以安全地对可变字符串切片进行的所有操作都可以通过这个包进行,比如将它们转换为小写或大写。这个包允许通过字符索引(而不是字节索引)进行分割和切片,替换字符串和使用字符引用。

字符串切片的所有函数都可以在包根处或作为StrExt特质的实例方法使用。

cargo add mut-str

示例

use mut_str::StrExt;

let mut welcome = Box::<str>::from("Hello, World!");

// Split by character index
let (l, r) = welcome.char_split_at_mut(7).unwrap();
assert_eq!(l, "Hello, ");
assert_eq!(r, "World!");

// Replace string slices
l.replace_with("mut-str").unwrap();
assert_eq!(l, "mut-str");

// Replace with padding
let sub = r.replace_with_pad_left_char("👑!", ' ').unwrap();
assert_eq!(sub, "👑!");
assert_eq!(r, " 👑!");

assert_eq!(&*welcome, "mut-str 👑!");

// Get character references
let crown = welcome.get_char_mut(8).unwrap();
assert_eq!(crown, '👑');

// Mutate characters
crown.replace('🌍').unwrap();
assert_eq!(crown, '🌍');

// Slice by character index
let l = welcome.char_slice_mut(..7).unwrap();
l.replace_with_pad_left_space("👋").unwrap();

assert_eq!(&*welcome, "   👋 🌍!");

最新文档
示例

mut-str 在crates.io上
mut-str 在GitHub上

功能

  • alloc(默认启用)添加了需要alloc库的实现。
  • std(默认启用,需要alloc)添加了针对标准库的特定实现。
  • nightly(需要rust nightly)见下文。

为了使此包与no-std兼容,请禁用std功能。

cargo add mut-str --no-default-features
cargo add mut-str --no-default-features --features=alloc

夜间版本变更

  • 使用extern_types功能来修复指针来源问题。
  • 为未启用std的错误实现Error

许可证

mut-str 可根据您的选择,在Apache License Version 2.0MIT许可证下进行双重许可。

无运行时依赖

功能