#![allow(unused)]fnmain() {
impl OsStr {
pubfncontains<'a, P>(&'aself, pat: P) -> boolwhere
P: Pattern<&'aSelf>;
pubfnstarts_with<'a, P>(&'aself, pat: P) -> boolwhere
P: Pattern<&'aSelf>;
pubfnends_with<'a, P>(&'aself, pat: P) -> boolwhere
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
pubfnfind<'a, P>(&'aself, pat: P) -> Option<usize>
where
P: Pattern<&'aSelf>;
pubfnrfind<'a, P>(&'aself, pat: P) -> Option<usize>
where
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
/// Finds the first range of this string which contains the pattern.////// # Examples////// ```rust/// let path = OsStr::new("/usr/bin/bash");/// let range = path.find_range("/b");/// assert_eq!(range, Some(4..6));/// assert_eq!(path[range.unwrap()], OsStr::new("/b"));/// ```pubfnfind_range<'a, P>(&'aself, pat: P) -> Option<Range<usize>>
where
P: Pattern<&'aSelf>;
/// Finds the last range of this string which contains the pattern.////// # Examples////// ```rust/// let path = OsStr::new("/usr/bin/bash");/// let range = path.rfind_range("/b");/// assert_eq!(range, Some(8..10));/// assert_eq!(path[range.unwrap()], OsStr::new("/b"));/// ```pubfnrfind_range<'a, P>(&'aself, pat: P) -> Option<Range<usize>>
where
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
// (Note: these should return a concrete iterator type instead of `impl Trait`.// For ease of explanation the concrete type is not listed here.)pubfnsplit<'a, P>(&'aself, pat: P) -> implIterator<Item = &'aSelf>
where
P: Pattern<&'aSelf>;
pubfnrsplit<'a, P>(&'aself, pat: P) -> implIterator<Item = &'aSelf>
where
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
pubfnsplit_terminator<'a, P>(&'aself, pat: P) -> implIterator<Item = &'aSelf>
where
P: Pattern<&'aSelf>;
pubfnrsplit_terminator<'a, P>(&'aself, pat: P) -> implIterator<Item = &'aSelf>
where
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
pubfnsplitn<'a, P>(&'aself, n: usize, pat: P) -> implIterator<Item = &'aSelf>
where
P: Pattern<&'aSelf>;
pubfnrsplitn<'a, P>(&'aself, n: usize, pat: P) -> implIterator<Item = &'aSelf>
where
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
pubfnmatches<'a, P>(&'aself, pat: P) -> implIterator<Item = &'aSelf>
where
P: Pattern<&'aSelf>;
pubfnrmatches<'a, P>(&self, pat: P) -> implIterator<Item = &'aSelf>
where
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
pubfnmatch_indices<'a, P>(&self, pat: P) -> implIterator<Item = (usize, &'aSelf)>
where
P: Pattern<&'aSelf>;
pubfnrmatch_indices<'a, P>(&self, pat: P) -> implIterator<Item = (usize, &'aSelf)>
where
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
// this is newpubfnmatch_ranges<'a, P>(&'aself, pat: P) -> implIterator<Item = (Range<usize>, &'aSelf)>
where
P: Pattern<&'aSelf>;
// this is newpubfnrmatch_ranges<'a, P>(&'aself, pat: P) -> implIterator<Item = (Range<usize>, &'aSelf)>
where
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
pubfntrim_matches<'a, P>(&'aself, pat: P) -> &'aSelfwhere
P: Pattern<&'aSelf>,
P::Searcher: DoubleEndedSearcher<&'aSelf>;
pubfntrim_left_matches<'a, P>(&'aself, pat: P) -> &'aSelfwhere
P: Pattern<&'aSelf>;
pubfntrim_right_matches<'a, P>(&'aself, pat: P) -> &'aSelfwhere
P: Pattern<&'aSelf>,
P::Searcher: ReverseSearcher<&'aSelf>;
pubfnreplace<'a, P>(&'aself, from: P, to: &'aSelf) -> Self::Owned
where
P: Pattern<&'aSelf>;
pubfnreplacen<'a, P>(&'aself, from: P, to: &'aSelf, count: usize) -> Self::Owned
where
P: Pattern<&'aSelf>;
}
}
Wealsoallow
許可する、可能にする
slicinganOsStr.
#![allow(unused)]fnmain() {
impl Index<RangeFull> for OsStr { ... }
impl Index<RangeFrom<usize>> for OsStr { ... }
impl Index<RangeTo<usize>> for OsStr { ... }
impl Index<Range<usize>> for OsStr { ... }
}
Example:
#![allow(unused)]fnmain() {
// (assume we are on Windows)let path = OsStr::new(r"C:\Users\Admin\😀\😁😂😃😄.txt");
// can use starts_with, ends_withassert!(path.starts_with(OsStr::new(r"C:\")));
assert!(path.ends_with(OsStr::new(".txt"));
// can use rfind_range to get the range of substringlet last_backslash = path.rfind_range(OsStr::new(r"\")).unwrap();
assert_eq!(last_backslash, 16..17);
// can perform slicing.let file_name = &path[last_backslash.end..];
// can perform splitting, even if it results in invalid Unicode!letmut parts = file_name.split(&*OsString::from_wide(&[0xd83d]));
assert_eq!(parts.next(), Some(OsStr::new("")));
assert_eq!(parts.next(), Some(&*OsString::from_wide(&[0xde01])));
assert_eq!(parts.next(), Some(&*OsString::from_wide(&[0xde02])));
assert_eq!(parts.next(), Some(&*OsString::from_wide(&[0xde03])));
assert_eq!(parts.next(), Some(&*OsString::from_wide(&[0xde04, 0x2e, 0x74, 0x78, 0x74])));
assert_eq!(parts.next(), None);
}
#![allow(unused)]fnmain() {
// (actual code implementing `.split()`)matchself.matcher.next_match() {
Some((a, b)) => unsafe {
let haystack = self.matcher.haystack();
let a = H::start_to_end_cursor(&haystack, a);
let b = H::end_to_start_cursor(&haystack, b);
let elt = H::range_to_self(haystack, self.start, a);
// ^ without `start_to_end_cursor`, the slice `elt` may be short by 2 bytesself.start = b;
// ^ without `end_to_start_cursor`, the next starting position may skip 2 bytesSome(elt)
},
None => self.get_end(),
}
}
#![allow(unused)]fnmain() {
let needle = OsString::from_wide(&[0xdc00]);
let haystack = OsStr::new("\u{10000}a");
let index = haystack.find(&needle).unwrap();
let matched = &haystack[index..(index + needle.len())];
// `matched` will contain "\u{dc00}a" instead of "\u{dc00}".}
#![allow(unused)]fnmain() {
let s = OsStr::new("\u{10000}");
assert_eq!(s.len(), 4);
let index = s.find('\u{dc00}').unwrap();
let right = &s[index..]; // [90 80 80]let left = &s[..index]; // [f0 90 80]}