Macro dict
macro_rules! dict {
($($key:expr => $value:expr),* $(,)?) => { ... };
(= $($key:expr => $value:expr),* $(,)?) => { ... };
}Expand description
Constructs typed Dictionary<K, V> literals, close to Godot’s own syntax.
Any value can be used as a key, but to use an expression you need to surround it
in () or {}.
§Type annotation
The macro creates a typed Dictionary<K, V>. You must provide an explicit type annotation
to specify K and V. Keys must implement AsArg<K> and values must implement AsArg<V>.
The = prefix (dict! {= ...}) switches to AsDirectElement, enabling unambiguous type inference
without an explicit type annotation. This covers types like i32, &str (inferred as GString), &GString, etc.
§Example
use godot::builtin::*;
// Type annotation required, as the same initializer can be mapped to different types.
let d: Dictionary<GString, i64> = dict! { "key1" => 10, "key2" => 20 };
let d: Dictionary<StringName, i64> = dict! { "key1" => 10, "key2" => 20 };
let d: Dictionary<GString, Variant> = dict! { "key1" => 10, "key2" => 20 };
// `=` prefix: most common conversion, no type annotation needed.
let d = dict! {= "key1" => 10, "key2" => 20 }; // Dictionary<GString, i32>.§See also
For untyped dictionaries, use vdict!.
For arrays, similar macros array! and varray! exist.