Trait OsStrExt
trait OsStrExt: private::Sealed
String-like methods for OsStr
Required Methods
fn try_str(self: &Self) -> Result<&str, Utf8Error>Converts to a string slice.
The
Utf8Erroris guaranteed to have a valid UTF8 boundary in itsvalid_up_to()fn contains(self: &Self, needle: &str) -> boolReturns
trueif the given pattern matches a sub-slice of this string slice.Returns
falseif it does not.Examples
use OsStrExt as _; let bananas = new; assert!; assert!;fn find(self: &Self, needle: &str) -> Option<usize>Returns the byte index of the first character of this string slice that matches the pattern.
Returns
Noneif the pattern doesn't match.Examples
use OsStrExt as _; let s = new; assert_eq!; assert_eq!; assert_eq!;Not finding the pattern:
use OsStrExt as _; let s = new; assert_eq!;fn strip_prefix(self: &Self, prefix: &str) -> Option<&OsStr>Returns a string slice with the prefix removed.
If the string starts with the pattern
prefix, returns substring after the prefix, wrapped inSome.If the string does not start with
prefix, returnsNone.Examples
use OsStr; use OsStrExt as _; assert_eq!; assert_eq!; assert_eq!;fn starts_with(self: &Self, prefix: &str) -> boolReturns
trueif the given pattern matches a prefix of this string slice.Returns
falseif it does not.Examples
use OsStrExt as _; let bananas = new; assert!; assert!;fn split<'s, 'n>(self: &'s Self, needle: &'n str) -> Split<'s, 'n>An iterator over substrings of this string slice, separated by characters matched by a pattern.
Examples
Simple patterns:
use OsStr; use OsStrExt as _; let v: = new.split.collect; assert_eq!; let v: = new.split.collect; assert_eq!; let v: = new.split.collect; assert_eq!; let v: = new.split.collect; assert_eq!;If a string contains multiple contiguous separators, you will end up with empty strings in the output:
use OsStr; use OsStrExt as _; let x = new; let d: = x.split.collect; assert_eq!;Contiguous separators are separated by the empty string.
use OsStr; use OsStrExt as _; let x = new; let d: = x.split.collect; assert_eq!;Separators at the start or end of a string are neighbored by empty strings.
use OsStr; use OsStrExt as _; let d: = new.split.collect; assert_eq!;When the empty string is used as a separator, it panics
use std::ffi::OsStr; use clap_lex::OsStrExt as _; let f: Vec<_> = OsStr::new("rust").split("").collect(); assert_eq!(f, &[OsStr::new(""), OsStr::new("r"), OsStr::new("u"), OsStr::new("s"), OsStr::new("t"), OsStr::new("")]);Contiguous separators can lead to possibly surprising behavior when whitespace is used as the separator. This code is correct:
use OsStr; use OsStrExt as _; let x = new; let d: = x.split.collect; assert_eq!;It does not give you:
assert_eq!;Use
split_whitespacefor this behavior.fn split_once(self: &Self, needle: &str) -> Option<(&OsStr, &OsStr)>Splits the string on the first occurrence of the specified delimiter and returns prefix before delimiter and suffix after delimiter.
Examples
use OsStr; use OsStrExt as _; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
Implementors
impl OsStrExt for OsStr