#[repr(transparent)]pub struct PyString(PyAny);
Expand description
Represents a Python string
(a Unicode string object).
Values of this type are accessed via PyO3’s smart pointers, e.g. as
Py<PyString>
or Bound<'py, PyString>
.
For APIs available on str
objects, see the PyStringMethods
trait which is implemented for
Bound<'py, PyString>
.
§Equality
For convenience, [Bound<'py, PyString>
] implements PartialEq<str>
to allow comparing the
data in the Python string to a Rust UTF-8 string slice.
This is not always the most appropriate way to compare Python strings, as Python string subclasses
may have different equality semantics. In situations where subclasses overriding equality might be
relevant, use PyAnyMethods::eq
, at cost of the additional overhead of a Python method call.
use pyo3::types::PyString;
let py_string = PyString::new(py, "foo");
// via PartialEq<str>
assert_eq!(py_string, "foo");
// via Python equality
assert!(py_string.as_any().eq("foo").unwrap());
Tuple Fields§
§0: PyAny
Implementations§
Source§impl PyString
impl PyString
Sourcepub fn new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString>
pub fn new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString>
Creates a new Python string object.
Panics if out of memory.
Sourcepub fn new_bound<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString>
👎Deprecated since 0.23.0: renamed to PyString::new
pub fn new_bound<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString>
PyString::new
Deprecated name for PyString::new
.
Sourcepub fn intern<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString>
pub fn intern<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString>
Intern the given string
This will return a reference to the same Python string object if called repeatedly with the same string.
Note that while this is more memory efficient than PyString::new_bound
, it unconditionally allocates a
temporary Python string object and is thereby slower than PyString::new_bound
.
Panics if out of memory.
Sourcepub fn intern_bound<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString>
👎Deprecated since 0.23.0: renamed to PyString::intern
pub fn intern_bound<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString>
PyString::intern
Deprecated name for PyString::intern
.
Sourcepub fn from_object<'py>(
src: &Bound<'py, PyAny>,
encoding: &str,
errors: &str,
) -> PyResult<Bound<'py, PyString>>
pub fn from_object<'py>( src: &Bound<'py, PyAny>, encoding: &str, errors: &str, ) -> PyResult<Bound<'py, PyString>>
Attempts to create a Python string from a Python bytes-like object.
Sourcepub fn from_object_bound<'py>(
src: &Bound<'py, PyAny>,
encoding: &str,
errors: &str,
) -> PyResult<Bound<'py, PyString>>
👎Deprecated since 0.23.0: renamed to PyString::from_object
pub fn from_object_bound<'py>( src: &Bound<'py, PyAny>, encoding: &str, errors: &str, ) -> PyResult<Bound<'py, PyString>>
PyString::from_object
Deprecated name for PyString::from_object
.
Methods from Deref<Target = PyAny>§
#[doc(hidden)] pub const _PYO3_DEF: AddTypeToModule<Self> = _
Trait Implementations§
Source§impl PyTypeInfo for PyString
impl PyTypeInfo for PyString
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
Source§fn is_type_of_bound(obj: &Bound<'_, PyAny>) -> bool
fn is_type_of_bound(obj: &Bound<'_, PyAny>) -> bool
PyTypeInfo::is_type_of
PyTypeInfo::is_type_of
.Source§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
Source§fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType>
fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType>
PyTypeInfo::type_object
PyTypeInfo::type_object
.Source§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object
is an instance of this type or a subclass of this type.Source§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object
is an instance of this type.Source§fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool
PyTypeInfo::is_exact_type_of
PyTypeInfo::is_exact_type_of
.impl DerefToPyAny for PyString
Auto Trait Implementations§
impl !Freeze for PyString
impl !RefUnwindSafe for PyString
impl !Send for PyString
impl !Sync for PyString
impl Unpin for PyString
impl UnwindSafe for PyString
Blanket Implementations§
Source§impl<T> AssertNotZeroSized for T
impl<T> AssertNotZeroSized for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more