Crates.io | indexed_string |
lib.rs | indexed_string |
version | 0.1.0 |
source | src |
created_at | 2022-03-25 15:02:47.858275 |
updated_at | 2022-03-25 15:02:47.858275 |
description | A naive implementation for rust's string to support index, works with unicode in O(n) time. |
homepage | https://github.com/ObjectiveCharm/indexed_string |
repository | |
max_upload_size | |
id | 556257 |
size | 20,235 |
Rust's stdlib does not support index and access string with vec-like subscript syntax. Slicing string could also be "dangerous" for it will panic and crash if you achieve the middle of single utf-8 char.
"You should use ranges to create string slices with caution, because doing so can crash your program"
So I try to make this small and simple library to make things simpler and less tiring.
The library offers wrapper type IndexedString
based on vec witch supports indexing, iterating and modifying string.
And it can convert from and to string and string slice simply and safely like vec.
Simply add dependency to cargo.toml
[dependencies]
#... other
indexed_string = "0.1.0"
/// import mod to scope
use indexed_string::indexed_string::IndexedString;
let str = "Stand with Ukraine";
/// convert &str to IndexedString
let indexed_string = IndexedString::from(str);
/// immutably access string
assert_eq!(indexed_string[1], "t");
/// mutably access string
indexed_string[0] = "s";
/// Get modified new string
println!("{}", indexed_string.to_string);
As its description, it is just a naive implementation(I am not familiar with rust)
But it is tested and works :)
Feel free to fork it and open pull request for refactoring, bug fix and so on.
MIT for detail please visit the website