pub struct Ncname(/* private fields */);
Expand description
String slice for NCName
.
Implementations§
Source§impl Ncname
impl Ncname
Sourcepub fn from_str(s: &str) -> Result<&Self, NameError>
pub fn from_str(s: &str) -> Result<&Self, NameError>
Creates a new &Ncname
.
§Failures
Fails if the given string is not a valid NCName
.
§Examples
let name = Ncname::from_str("hello")?;
assert_eq!(name, "hello");
assert!(Ncname::from_str("").is_err(), "Empty string is not an NCName");
assert!(Ncname::from_str("foo bar").is_err(), "Whitespace is not allowed");
assert!(Ncname::from_str("foo:bar").is_err(), "Colon is not allowed");
assert!(Ncname::from_str("0foo").is_err(), "ASCII digit at the beginning 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 name = Ncname::from_str("hello")?;
assert_eq!(name, "hello");
let s: &str = name.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 name = Ncname::from_str("foo")?;
assert_eq!(name.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 Ncname
and returns the value and the rest input.
§Exmaples
let input = "hello:world";
let expected = Ncname::from_str("hello").expect("valid NCName");
assert_eq!(
Ncname::parse_next(input),
Ok((expected, ":world"))
);
let input = "012";
assert!(Ncname::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<Ncname>
into a Box<str>
without copying or allocating.
§Examples
let name = Ncname::from_str("ncname")?;
let boxed_name: Box<Ncname> = name.into();
assert_eq!(&*boxed_name, name);
let boxed_str: Box<str> = boxed_name.into_boxed_str();
assert_eq!(&*boxed_str, name.as_str());