#string #const-generics #stack-allocated #fixed-size #length #fixed-length #version

no_std_strings

crate fixedstr 的 no_std 版本:可以复制和栈分配的固定最大大小的字符串

3 个版本

0.1.31 2023年9月4日
0.1.3 2023年6月27日
0.1.2 2023年6月26日
0.1.1 2023年6月26日
0.1.0 2023年6月25日

#974 in Rust 模式

每月下载 43

MIT 许可证

48KB
806 代码行

#![no_std] 版本的 crate fixedstr:使用 const generics 复制和栈分配固定最大长度的字符串的库。

此 crate 将不再更新。相反,fixedstr crate 的 0.4 版本现在支持 no_std 作为选项。**

示例

  let a:str8 = str8::from("abcdef"); //a str8 can hold up to 7 bytes
  let a2 = a;  // copied, not moved
  let ab = a.substr(1,5);  // copies substring to new string
  assert_eq!(ab, "bcde");  // can compare for equality with &str
  assert_eq!(ab.len(),4);
  println!("str8: {}", &a);   // impls Display
  assert_eq!(&a[..3], "abc"); // impls Index for Range types
  assert!(a<ab); // and Ord, Hash, Debug, Display, Eq, other common traits
  let astr:&str = a.to_str(); // convert to &str (zero copy)
  let azstr:zstr<16> = zstr::from(a); //zstr is a zero-terminated string
  let a32:str32 = a.resize(); // same kind of string but with 31-byte capacity  
  let mut u = str8::from("aλb"); //unicode support
  assert_eq!(u.nth(1), Some('λ'));  // get nth character
  assert_eq!(u.nth_ascii(3), 'b');  // get nth byte as ascii character
  assert!(u.set(1,'μ'));  // changes a character of the same character class
  assert!(!u.set(1,'c')); // .set returns false on failure
  assert!(u.set(2,'c'));
  assert_eq!(u, "aμc");
  assert_eq!(u.len(),4);  // length in bytes
  assert_eq!(u.charlen(),3);  // length in chars
  let mut ac:str16 = a.reallocate().unwrap(); //copies to larger capacity type
  let remainder = ac.push("ghijklmnopq"); //append up to capacity, returns remainder
  assert_eq!(ac.len(),15);
  assert_eq!(remainder, "pq");
  ac.truncate(9);  // keep first 9 chars
  assert_eq!(&ac,"abcdefghi");
  let (upper,lower) = (str8::make("ABC"), str8::make("abc"));
  assert_eq!(upper, lower.to_ascii_upper()); // no owned String needed

  let c1 = str8::from("abcd"); // string concatenation with + for strN types  
  let c2 = str8::from("xyz");
  let c3 = c1 + c2;           
  assert_eq!(c3,"abcdxyz");
  assert_eq!(c3.capacity(),15);  // type of c3 is str16

  let c4 = str_format!(str16,"abc {}{}{}",1,2,3); // impls std::fmt::Write
  assert_eq!(c4,"abc 123");  //str_format! truncates if capacity exceeded
  let c5 = try_format!(str8,"abcdef{}","ghijklmn");
  assert!(c5.is_none());  // try_format! returns None if capacity exceeded

有关详细信息,请参阅 文档

依赖项

~165KB