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§
Sourcefn try_substr<'t>(&'t self, idx: impl StringIndex) -> Result<&'t str, &'t str>
fn try_substr<'t>(&'t self, idx: impl StringIndex) -> Result<&'t str, &'t str>
Sourcefn try_substr_back<'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>
Sourcefn merge<'t>(&'t self, first: &str, second: &str) -> Option<&'t str>
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§
Sourcefn substr<'t>(&'t self, idx: impl StringIndex) -> &'t str
fn substr<'t>(&'t self, idx: impl StringIndex) -> &'t str
Obtain a substring with a given index or range.
Sourcefn substr_back<'t>(&'t self, idx: impl StringIndex) -> &'t str
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.