pub struct GString { /* private fields */ }
Expand description
Godot’s reference counted string type.
This is the Rust binding of GDScript’s String
type. It represents the native string class used within the Godot engine,
and as such has different memory layout and characteristics than std::string::String
.
GString
uses copy-on-write semantics and is cheap to clone. Modifying a string may trigger a copy, if that instance shares
its backing storage with other strings.
Note that GString
is not immutable, but it offers a very limited set of write APIs. Most operations return new strings.
In order to modify Godot strings, it’s often easiest to convert them to Rust strings, perform the modifications and convert back.
§GString
vs. String
When interfacing with the Godot engine API, you often have the choice between String
and GString
. In user-declared methods
exposed to Godot through the #[func]
attribute, both types can be used as parameters and return types, and conversions
are done transparently. For auto-generated binding APIs in godot::classes
, both parameters and return types are GString
.
In the future, we will likely declare parameters as impl Into<GString>
, allowing String
or &str
to be passed.
As a general guideline, use GString
if:
- your strings are very large, so you can avoid copying them
- you need specific operations only available in Godot (e.g.
sha256_text()
,c_escape()
, …) - you primarily pass them between different Godot APIs, without string processing in user code
Use Rust’s String
if:
- you need to modify the string
- you would like to decouple part of your code from Godot (e.g. independent game logic, standalone tests)
- you want a standard type for interoperability with third-party code (e.g.
regex
crate) - you have a large number of method calls per string instance (which are more expensive due to indirectly calling into Godot)
- you need UTF-8 encoding (
GString
’s encoding is platform-dependent and unspecified)
§Other string types
Godot also provides two separate string classes with slightly different semantics: StringName
and NodePath
.
§Null bytes
Note that Godot ignores any bytes after a null-byte. This means that for instance "hello, world!"
and "hello, world!\0 ignored by Godot"
will be treated as the same string if converted to a GString
.
Implementations§
§impl GString
impl GString
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn chars_checked(&self) -> &[char]
👎Deprecated: Use chars()
instead.
Since version 4.1, Godot ensures valid UTF-32, checked and unchecked overloads are no longer needed.
For details, see godotengine/godot#74760.
pub fn chars_checked(&self) -> &[char]
chars()
instead.
Since version 4.1, Godot ensures valid UTF-32, checked and unchecked overloads are no longer needed.
For details, see godotengine/godot#74760.Gets the internal chars slice from a GString
.
Note: This operation is O(n). Consider using chars_unchecked
if you can make sure the string is a valid UTF-32.
pub unsafe fn chars_unchecked(&self) -> &[char]
👎Deprecated: Use chars()
instead.
Since version 4.1, ensures valid UTF-32, checked and unchecked overloads are no longer needed.
For details, see godotengine/godot#74760.
pub unsafe fn chars_unchecked(&self) -> &[char]
chars()
instead.
Since version 4.1, ensures valid UTF-32, checked and unchecked overloads are no longer needed.
For details, see godotengine/godot#74760.Trait Implementations§
§impl Export for GString
impl Export for GString
§fn default_export_info() -> PropertyHintInfo
fn default_export_info() -> PropertyHintInfo
§impl Extend<GString> for PackedStringArray
impl Extend<GString> for PackedStringArray
Extends aPackedStringArray
with the contents of an iterator
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = GString>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = GString>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl From<&GString> for StringName
impl From<&GString> for StringName
§fn from(string: &GString) -> StringName
fn from(string: &GString) -> StringName
§impl From<&StringName> for GString
impl From<&StringName> for GString
§fn from(string: &StringName) -> GString
fn from(string: &StringName) -> GString
§impl From<GString> for StringName
impl From<GString> for StringName
§fn from(string: GString) -> StringName
fn from(string: GString) -> StringName
Converts this GString
to a StringName
.
This is identical to StringName::from(&string)
, and as such there is no performance benefit.
§impl From<StringName> for GString
impl From<StringName> for GString
§fn from(string_name: StringName) -> GString
fn from(string_name: StringName) -> GString
Converts this StringName
to a GString
.
This is identical to GString::from(&string_name)
, and as such there is no performance benefit.
§impl FromGodot for GString
impl FromGodot for GString
§fn try_from_godot(
via: <GString as GodotConvert>::Via,
) -> Result<GString, ConvertError>
fn try_from_godot( via: <GString as GodotConvert>::Via, ) -> Result<GString, ConvertError>
Err
on failure.§fn from_godot(via: Self::Via) -> Self
fn from_godot(via: Self::Via) -> Self
§fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
Variant
, returning Err
on failure.§fn from_variant(variant: &Variant) -> Self
fn from_variant(variant: &Variant) -> Self
§impl FromIterator<GString> for PackedStringArray
impl FromIterator<GString> for PackedStringArray
Creates a PackedStringArray
from an iterator.
§fn from_iter<I>(iter: I) -> PackedStringArraywhere
I: IntoIterator<Item = GString>,
fn from_iter<I>(iter: I) -> PackedStringArraywhere
I: IntoIterator<Item = GString>,
§impl GodotConvert for GString
impl GodotConvert for GString
§impl Ord for GString
impl Ord for GString
§impl PartialOrd for GString
impl PartialOrd for GString
§fn partial_cmp(&self, other: &GString) -> Option<Ordering>
fn partial_cmp(&self, other: &GString) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more§impl ToGodot for GString
impl ToGodot for GString
§fn to_godot(&self) -> <GString as GodotConvert>::Via
fn to_godot(&self) -> <GString as GodotConvert>::Via
§fn into_godot(self) -> <GString as GodotConvert>::Via
fn into_godot(self) -> <GString as GodotConvert>::Via
§fn to_variant(&self) -> Variant
fn to_variant(&self) -> Variant
§impl TypeStringHint for GString
impl TypeStringHint for GString
§fn type_string() -> String
fn type_string() -> String
§impl Var for GString
impl Var for GString
fn get_property(&self) -> <GString as GodotConvert>::Via
fn set_property(&mut self, value: <GString as GodotConvert>::Via)
fn property_hint() -> PropertyHintInfo
impl ArrayElement for GString
impl Eq for GString
impl GodotType for GString
Auto Trait Implementations§
impl Freeze for GString
impl RefUnwindSafe for GString
impl !Send for GString
impl !Sync for GString
impl Unpin for GString
impl UnwindSafe for GString
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)