mod range;
pub trait Slice {
fn slice(&self, r: impl range::Range) -> String;
}
impl Slice for String {
fn slice(&self, r: impl range::Range) -> String {
let (x, y) = range::get_indices(&self, r);
let mut new = String::new();
for (i, c) in self.char_indices() {
if i >= x && i < y {
new.push(c);
}
}
new
}
}
impl Slice for str {
fn slice(&self, r: impl range::Range) -> String {
let (x, y) = range::get_indices(&self, r);
let mut new = String::new();
for (i, c) in self.char_indices() {
if i >= x && i < y {
new.push(c);
}
}
new
}
}
#[test]
fn test() {
let s = "hello🙃world!";
println!("{}", s.slice(6..));
}