pub struct Nmtoken(/* private fields */);Expand description
String slice for Nmtoken.
Implementations§
Source§impl Nmtoken
impl Nmtoken
Sourcepub fn from_str(s: &str) -> Result<&Self, NameError>
pub fn from_str(s: &str) -> Result<&Self, NameError>
Creates a new &Nmtoken.
§Failures
Fails if the given string is not a valid Nmtoken.
§Examples
assert_eq!(Nmtoken::from_str("hello")?, "hello");
assert_eq!(Nmtoken::from_str("012")?, "012");
assert!(Nmtoken::from_str("").is_err(), "Empty string is not an Nmtoken");
assert!(Nmtoken::from_str("foo bar").is_err(), "Whitespace is not allowed");Sourcepub unsafe fn new_unchecked(s: &str) -> &Self
pub unsafe fn new_unchecked(s: &str) -> &Self
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the string as &str.
§Examples
let tok = Nmtoken::from_str("hello")?;
assert_eq!(tok, "hello");
let s: &str = tok.as_str();
assert_eq!(s, "hello");Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the string in bytes.
§Examples
let s = Nmtoken::from_str("foo")?;
assert_eq!(s.len(), 3);Sourcepub fn parse_next(s: &str) -> Result<(&Self, &str), NameError>
pub fn parse_next(s: &str) -> Result<(&Self, &str), NameError>
Parses the leading Nmtoken and returns the value and the rest input.
§Exmaples
let input = "hello, world";
let expected = Nmtoken::from_str("hello").expect("valid Nmtoken");
assert_eq!(
Nmtoken::parse_next(input),
Ok((expected, ", world"))
);let input = " ";
assert!(Nmtoken::parse_next(input).is_err());Sourcepub fn into_boxed_str(self: Box<Self>) -> Box<str>
pub fn into_boxed_str(self: Box<Self>) -> Box<str>
Converts a Box<Nmtoken> into a Box<str> without copying or allocating.
§Examples
let name = Nmtoken::from_str("ncname")?;
let boxed_name: Box<Nmtoken> = name.into();
assert_eq!(&*boxed_name, name);
let boxed_str: Box<str> = boxed_name.into_boxed_str();
assert_eq!(&*boxed_str, name.as_str());