pub struct PyString(/* private fields */);
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());
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
.
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> 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