pub struct GodotString(_);
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.

GodotString is reference-counted like most types in godot-rust. Thus, its clone() method does not return a copy of the string, but simply another instance which shares the same backing string. Furthermore, GodotString is immutable and does not offer any write APIs. If you need to modify Godot strings, convert them to Rust strings, perform the modifications and convert back. In GDScript, strings have copy-on-write semantics, which guarantees that GodotString instances in Rust are independent of their GDScript counterparts. A modification of a string in GDScript (which was previously passed to Rust) will not be reflected in Rust.

When interfacing with the Godot engine API through godot-rust, you often have the choice between std::string::String and gdnative::core_types::GodotString. In user methods that are exposed to Godot through the #[export] macro, both types can be used as parameters and return types, and any conversions are done transparently. For auto-generated binding APIs in gdnative::api, return types are GodotString, but parameters are declared impl Into<GodotString>, allowing String or &str to be passed. In addition, the two types can always be explicitly converted using GodotString::from_str() and GodotString::display/to_string().

As a general guideline, use GodotString 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 (GodotString’s encoding is platform-dependent and unspecified)

Implementations

Formats the string by replacing all occurrences of a key in the string with the corresponding value. The method can handle arrays or dictionaries for the key/value pairs.

Arrays can be used as key, index, or mixed style (see below examples). Order only matters when the index or mixed style of Array is used.

Examples
// Array values, index style
let template = GodotString::from("{0} {1}");
let data = VariantArray::new();
data.push("foo");
data.push("bar");

let formatted = template.format(&data.into_shared().to_variant());
godot_print!("{}", formatted); // "foo bar"
// Dictionary values
let template = GodotString::from("foo {bar}");
let data = Dictionary::new();
data.insert("bar", "baz");

let formatted = template.format(&data.into_shared().to_variant());
godot_print!("{}", formatted); // "foo baz"

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

AddAssign implementations copy the strings’ contents since GodotString is immutable.

Performs the += operation. Read more

AddAssign implementations copy the strings’ contents since GodotString is immutable.

Performs the += operation. Read more

AddAssign implementations copy the strings’ contents since GodotString is immutable.

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

A type-specific hint type that is valid for the type being exported. Read more

Returns ExportInfo given an optional typed hint.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The index operator provides a low-level view of characters in Godot’s native encoding that doesn’t always correspond to Unicode code points one-to-one. This operation goes through FFI. For intensive string operations, consider converting to a Rust String first to avoid this cost.

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Creates a new reference to the underlying object.

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.