StringExt

Trait StringExt 

Source
pub trait StringExt {
    // Required methods
    fn try_substr<'t>(
        &'t self,
        idx: impl StringIndex,
    ) -> Result<&'t str, &'t str>;
    fn try_substr_back<'t>(
        &'t self,
        idx: impl StringIndex,
    ) -> Result<&'t str, &'t str>;
    fn merge<'t>(&'t self, first: &str, second: &str) -> Option<&'t str>;

    // Provided methods
    fn substr<'t>(&'t self, idx: impl StringIndex) -> &'t str { ... }
    fn substr_back<'t>(&'t self, idx: impl StringIndex) -> &'t str { ... }
}
Expand description

Extension methods for strings

Required Methods§

Source

fn try_substr<'t>(&'t self, idx: impl StringIndex) -> Result<&'t str, &'t str>

Try obtain a substring with a given index or range.

Returns Ok(&str) if the exact substring is found.

Returns Err(&str) if len is bound and less than len chars found.

Source

fn try_substr_back<'t>( &'t self, idx: impl StringIndex, ) -> Result<&'t str, &'t str>

Try obtain a substring with a given index or range in reverse.

Returns Ok(&str) if the exact substring is found.

Returns Err(&str) if len is bound and less than len chars found.

Source

fn merge<'t>(&'t self, first: &str, second: &str) -> Option<&'t str>

Concatenate adjacent substrings.

Returns None if first and second are not adjacent or first and second are not substrings of self.

§Examples

Correst usage:

let parent = "foobar";
let foo = &parent[0..3];
let bar = &parent[3..6];
assert_eq!(parent.merge(foo, bar), Some("foobar"));
 
// has to be in the correct order
assert_eq!(parent.merge(bar, foo), None);

Not substrings:

let parent = "foobar";
let foo = "foo";
let bar = "bar";
assert_eq!(parent.merge(foo, bar), None);

Not adjacent:

let parent = "foobar";
let fo = &parent[0..2];
let bar = &parent[3..6];
assert_eq!(parent.merge(fo, bar), None);

Overlapping:

let parent = "foobar";
let foob = &parent[0..4];
let obar = &parent[2..6];
assert_eq!(parent.merge(foob, obar), None);

Provided Methods§

Source

fn substr<'t>(&'t self, idx: impl StringIndex) -> &'t str

Obtain a substring with a given index or range.

Source

fn substr_back<'t>(&'t self, idx: impl StringIndex) -> &'t str

Obtain a substring with a given index or range in reverse.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> StringExt for T
where T: AsRef<str>,