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§

source§

impl GodotString

source

pub fn new() -> GodotString

source

pub fn from_str<S>(s: S) -> GodotStringwhere
    S: AsRef<str>,

source

pub fn len(&self) -> usize

source

pub fn is_empty(&self) -> bool

source

pub fn is_numeric(&self) -> bool

source

pub fn is_valid_float(&self) -> bool

source

pub fn is_valid_html_color(&self) -> bool

source

pub fn is_valid_identifier(&self) -> bool

source

pub fn is_valid_integer(&self) -> bool

source

pub fn is_valid_ip_address(&self) -> bool

source

pub fn is_resource_file(&self) -> bool

source

pub fn is_absolute_path(&self) -> bool

source

pub fn is_relative_path(&self) -> bool

source

pub fn to_f32(&self) -> f32

source

pub fn to_f64(&self) -> f64

source

pub fn to_i32(&self) -> i32

source

pub fn u32_hash(&self) -> u32

source

pub fn u64_hash(&self) -> u64

source

pub fn hex_to_int(&self) -> i32

source

pub fn hex_to_int_without_prefix(&self) -> i32

source

pub fn camelcase_to_underscore(&self) -> GodotString

source

pub fn camelcase_to_underscore_lowercased(&self) -> GodotString

source

pub fn capitalize(&self) -> GodotString

source

pub fn to_lowercase(&self) -> GodotString

source

pub fn to_uppercase(&self) -> GodotString

source

pub fn get_file(&self) -> GodotString

source

pub fn get_base_dir(&self) -> GodotString

source

pub fn get_basename(&self) -> GodotString

source

pub fn get_extension(&self) -> GodotString

source

pub fn simplify_path(&self) -> GodotString

source

pub fn sha256_text(&self) -> GodotString

source

pub fn md5_text(&self) -> GodotString

source

pub fn c_escape(&self) -> GodotString

source

pub fn c_escape_multiline(&self) -> GodotString

source

pub fn c_unescape(&self) -> GodotString

source

pub fn http_escape(&self) -> GodotString

source

pub fn http_unescape(&self) -> GodotString

source

pub fn json_escape(&self) -> GodotString

source

pub fn xml_escape(&self) -> GodotString

source

pub fn xml_escape_with_quotes(&self) -> GodotString

source

pub fn xml_unescape(&self) -> GodotString

source

pub fn percent_decode(&self) -> GodotString

source

pub fn percent_encode(&self) -> GodotString

source

pub fn is_valid_hex_number(&self, with_prefix: bool) -> bool

source

pub fn begins_with(&self, s: &GodotString) -> bool

source

pub fn ends_with(&self, s: &GodotString) -> bool

source

pub fn begins_with_c_str(&self, s: &CStr) -> bool

source

pub fn sub_string(&self, range: Range<usize>) -> GodotString

source

pub fn find(&self, what: &GodotString) -> i32

source

pub fn find_from(&self, what: &GodotString, from: i32) -> i32

source

pub fn find_last(&self, what: &GodotString) -> i32

source

pub fn format(&self, values: &Variant) -> GodotString

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§

source§

impl Add<&GodotString> for &GodotString

§

type Output = GodotString

The resulting type after applying the + operator.
source§

fn add(self, other: &GodotString) -> GodotString

Performs the + operation. Read more
source§

impl Add<GodotString> for GodotString

§

type Output = GodotString

The resulting type after applying the + operator.
source§

fn add(self, other: GodotString) -> GodotString

Performs the + operation. Read more
source§

impl<S> Add<S> for &GodotStringwhere
    S: AsRef<str>,

§

type Output = GodotString

The resulting type after applying the + operator.
source§

fn add(self, other: S) -> GodotString

Performs the + operation. Read more
source§

impl AddAssign<&GodotString> for GodotString

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

source§

fn add_assign(&mut self, other: &GodotString)

Performs the += operation. Read more
source§

impl AddAssign<GodotString> for GodotString

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

source§

fn add_assign(&mut self, other: GodotString)

Performs the += operation. Read more
source§

impl<S> AddAssign<S> for GodotStringwhere
    S: AsRef<str>,

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

source§

fn add_assign(&mut self, other: S)

Performs the += operation. Read more
source§

impl Clone for GodotString

source§

fn clone(&self) -> GodotString

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl CoerceFromVariant for GodotString

source§

impl Debug for GodotString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for GodotString

source§

fn default() -> GodotString

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

impl<'de> Deserialize<'de> for GodotString

source§

fn deserialize<D>(
    deserializer: D
) -> Result<GodotString, <D as Deserializer<'de>>::Error>where
    D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for GodotString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Drop for GodotString

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Export for GodotString

§

type Hint = StringHint

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

fn export_info(hint: Option<<GodotString as Export>::Hint>) -> ExportInfo

Returns ExportInfo given an optional typed hint.
source§

impl From<GodotString> for NodePath

source§

fn from(s: GodotString) -> NodePath

Converts to this type from the input type.
source§

impl From<NodePath> for GodotString

source§

fn from(p: NodePath) -> GodotString

Converts to this type from the input type.
source§

impl<S> From<S> for GodotStringwhere
    S: AsRef<str>,

source§

fn from(s: S) -> GodotString

Converts to this type from the input type.
source§

impl FromVariant for GodotString

source§

impl Hash for GodotString

source§

fn hash<H>(&self, state: &mut H)where
    H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where
    H: Hasher,
    Self: Sized,

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

impl Index<usize> for GodotString

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.

§

type Output = GodotChar

The returned type after indexing.
source§

fn index(&self, index: usize) -> &<GodotString as Index<usize>>::Output

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

impl NewRef for GodotString

source§

fn new_ref(&self) -> GodotString

Creates a new reference to the underlying object.
source§

impl Ord for GodotString

source§

fn cmp(&self, other: &GodotString) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere
    Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere
    Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere
    Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<GodotString> for GodotString

source§

fn eq(&self, other: &GodotString) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

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

impl PartialOrd<GodotString> for GodotString

source§

fn partial_cmp(&self, other: &GodotString) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl Serialize for GodotString

source§

fn serialize<S>(
    &self,
    serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
    S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl ToVariant for GodotString

source§

impl Eq for GodotString

source§

impl PoolElement for GodotString

source§

impl ToVariantEq for GodotString

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere
    Q: Eq + ?Sized,
    K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

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

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> OwnedToVariant for Twhere
    T: ToVariant,

source§

impl<T> ToOwned for Twhere
    T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for Twhere
    T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere
    T: for<'de> Deserialize<'de>,