@@ -23,6 +23,53 @@ use std::{fmt, ptr};
23
23
/// The keys and values of the dictionary are all `Variant`s, so they can be of different types.
24
24
/// Variants are designed to be generally cheap to clone.
25
25
///
26
+ /// # Dictionary example
27
+ ///
28
+ /// ```no_run
29
+ /// # use godot::prelude::*;
30
+ /// // Create empty dictionary and add key-values pairs.
31
+ /// let mut dict = Dictionary::new();
32
+ /// dict.set("str", "Hello");
33
+ /// dict.set("num", 23);
34
+ ///
35
+ /// // Keys don't need to be strings.
36
+ /// let coord = Vector2i::new(0, 1);
37
+ /// dict.set(coord, "Tile77");
38
+ ///
39
+ /// // Or create the same dictionary in a single expression.
40
+ /// let dict = dict! {
41
+ /// "str": "Hello",
42
+ /// "num": 23,
43
+ /// coord: "Tile77",
44
+ /// };
45
+ ///
46
+ /// // Access elements.
47
+ /// let value: Variant = dict.at("str");
48
+ /// let value: GString = dict.at("str").to(); // Variant::to() extracts GString.
49
+ /// let maybe: Option<Variant> = dict.get("absent_key");
50
+ ///
51
+ /// // Iterate over key-value pairs as (Variant, Variant).
52
+ /// for (key, value) in dict.iter_shared() {
53
+ /// println!("{key} => {value}");
54
+ /// }
55
+ ///
56
+ /// // Use typed::<K, V>() to get typed iterators.
57
+ /// for (key, value) in dict.iter_shared().typed::<GString, Variant>() {
58
+ /// println!("{key} => {value}");
59
+ /// }
60
+ ///
61
+ /// // Clone dictionary (shares the reference), and overwrite elements through clone.
62
+ /// let mut cloned = dict.clone();
63
+ /// cloned.remove("num");
64
+ ///
65
+ /// // Overwrite with set(); use insert() to get the previous value.
66
+ /// let prev = cloned.insert("str", "Goodbye"); // prev == Some("Hello")
67
+ ///
68
+ /// // Changes will be reflected in the original dictionary.
69
+ /// assert_eq!(dict.at("str"), "Goodbye".to_variant());
70
+ /// assert_eq!(dict.get("num"), None);
71
+ /// ```
72
+ ///
26
73
/// # Thread safety
27
74
///
28
75
/// The same principles apply as for [`VariantArray`]. Consult its documentation for details.
@@ -246,7 +293,7 @@ impl Dictionary {
246
293
/// Note that it's possible to modify the `Dictionary` through another reference while iterating over it. This will not result in
247
294
/// unsoundness or crashes, but will cause the iterator to behave in an unspecified way.
248
295
///
249
- /// Use `iter_shared().typed::<K, V>()` to iterate over `(K, V)` pairs instead.
296
+ /// Use `dict. iter_shared().typed::<K, V>()` to iterate over `(K, V)` pairs instead.
250
297
pub fn iter_shared ( & self ) -> Iter < ' _ > {
251
298
Iter :: new ( self )
252
299
}
@@ -259,7 +306,7 @@ impl Dictionary {
259
306
/// Note that it's possible to modify the `Dictionary` through another reference while iterating over it. This will not result in
260
307
/// unsoundness or crashes, but will cause the iterator to behave in an unspecified way.
261
308
///
262
- /// Use `.keys_shared.typed::<K>()` to iterate over `K` keys instead.
309
+ /// Use `dict .keys_shared() .typed::<K>()` to iterate over `K` keys instead.
263
310
pub fn keys_shared ( & self ) -> Keys < ' _ > {
264
311
Keys :: new ( self )
265
312
}
0 commit comments