pub struct PyUnicode(/* private fields */);
Expand description
Represents a Python string.
Corresponds to basestring
in Python 2, and str
in Python 3.
Implementations§
source§impl PyString
impl PyString
sourcepub fn new(py: Python<'_>, s: &str) -> PyString
pub fn new(py: Python<'_>, s: &str) -> PyString
Creates a new Python string object.
On Python 2.7, this function will create a byte string if the
feature py2-no-auto-unicode-promotion
is set, or the input
input string is ASCII-only; otherwise, the input string will be
converted to a unicode string.
Use PyUnicode::new()
to always create a unicode string.
On Python 3.x, this function always creates unicode str
objects.
Panics if out of memory.
sourcepub fn data(&self, py: Python<'_>) -> PyStringData<'_>
pub fn data(&self, py: Python<'_>) -> PyStringData<'_>
Gets the python string data in its underlying representation.
For Python 2 byte strings, this function always returns PyStringData::Utf8
,
even if the bytes are not valid UTF-8.
For unicode strings, returns the underlying representation used by Python.
sourcepub fn to_string(&self, py: Python<'_>) -> PyResult<Cow<'_, str>>
pub fn to_string(&self, py: Python<'_>) -> PyResult<Cow<'_, str>>
Convert the PyString
into a Rust string.
On Python 2.7, if the PyString
refers to a byte string,
it will be decoded using UTF-8.
Returns a UnicodeDecodeError
if the input is not valid unicode
(containing unpaired surrogates, or a Python 2.7 byte string that is
not valid UTF-8).
sourcepub fn to_string_lossy(&self, py: Python<'_>) -> Cow<'_, str>
pub fn to_string_lossy(&self, py: Python<'_>) -> Cow<'_, str>
Convert the PyString
into a Rust string.
On Python 2.7, if the PyString
refers to a byte string,
it will be decoded using UTF-8.
Unpaired surrogates and (on Python 2.7) invalid UTF-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
Trait Implementations§
source§impl<'s> FromPyObject<'s> for &'s PyString
impl<'s> FromPyObject<'s> for &'s PyString
source§impl<'s> FromPyObject<'s> for PyString
impl<'s> FromPyObject<'s> for PyString
source§impl PythonObject for PyString
impl PythonObject for PyString
source§unsafe fn unchecked_downcast_from(obj: PyObject) -> Self
unsafe fn unchecked_downcast_from(obj: PyObject) -> Self
Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type.
source§unsafe fn unchecked_downcast_borrow_from<'a>(obj: &'a PyObject) -> &'a Self
unsafe fn unchecked_downcast_borrow_from<'a>(obj: &'a PyObject) -> &'a Self
Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type.
source§fn into_object(self) -> PyObject
fn into_object(self) -> PyObject
source§impl PythonObjectWithCheckedDowncast for PyString
impl PythonObjectWithCheckedDowncast for PyString
source§fn downcast_from<'p>(
py: Python<'p>,
obj: PyObject
) -> Result<PyString, PythonObjectDowncastError<'p>>
fn downcast_from<'p>( py: Python<'p>, obj: PyObject ) -> Result<PyString, PythonObjectDowncastError<'p>>
source§fn downcast_borrow_from<'a, 'p>(
py: Python<'p>,
obj: &'a PyObject
) -> Result<&'a PyString, PythonObjectDowncastError<'p>>
fn downcast_borrow_from<'a, 'p>( py: Python<'p>, obj: &'a PyObject ) -> Result<&'a PyString, PythonObjectDowncastError<'p>>
source§impl PythonObjectWithTypeObject for PyString
impl PythonObjectWithTypeObject for PyString
source§fn type_object(py: Python<'_>) -> PyType
fn type_object(py: Python<'_>) -> PyType
source§impl ToPyObject for PyString
impl ToPyObject for PyString
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.