#[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: PyAnyImplementations§
Source§impl PyString
 
impl PyString
#[doc(hidden)] pub const _PYO3_DEF: AddTypeToModule<Self>
#[doc(hidden)] pub const _PYO3_INTROSPECTION_ID: &'static str = ":: std :: option :: Option :: Some(\"builtins\")PyString"
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 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, it unconditionally allocates a
temporary Python string object and is thereby slower than PyString::new.
Panics if out of memory.
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.
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(obj: &Bound<'_, PyAny>) -> bool
 
fn is_type_of(obj: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.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 moreSource§impl<T> PyTypeCheck for Twhere
    T: PyTypeInfo,
 
impl<T> PyTypeCheck for Twhere
    T: PyTypeInfo,
Source§impl<T> SizedTypeProperties for T
 
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)] const IS_ZST: bool = _
 
#[doc(hidden)] const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)] const LAYOUT: Layout = _
 
#[doc(hidden)] const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)] const MAX_SLICE_LEN: usize = _
 
#[doc(hidden)] const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read more