pub struct JSString { /* private fields */ }Expand description
A UTF16 character buffer.
The fundamental string representation in JavaScript. Since this is using a UTF16 encoding and Rust strings are using UTF8 encoding, converting between string representations is not cheap.
In this crate, implementations of the conversion traits
Into and From are provided for JSString. This allows
conversion from &str and String into JSString:
let j: JSString = "abc".into();Similarly, a JSString can be converted to a String
via a conversion trait or directly:
let j: JSString = "abc".into();
let s: String = (&j).into(); // Requires a reference.
let s: String = j.to_string();In this crate, functions that need a JSString use
generics so that they can take anything that can be
converted to a JSString instead. This allows the
caller to pass an &str or String, or to cache a
previously converted JSString and pass that directly.
A JSString is not a JSValue and so it can not be
passed where a JSValue is expected. Instead, it must
be boxed using JSValue::new_string.
Implementations§
Source§impl JSString
impl JSString
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the number of Unicode characters in this JavaScript string.
Remember that strings in JavaScript are UTF-16 encoded.
let str = JSString::from("😄");
// The JavaScript string length is 2, since it's UTF-16 encoded.
assert_eq!(str.len(), 2);
// But once encoded into UTF-8 as a Rust string, it's 4.
assert_eq!(str.to_string().len(), 4);