Repository: typst/typst
File: crates/typst-library/src/foundations/dict.rs (Rust)

Code before:
pub use crate::__dict as dict;

/// A map from string keys to values.
///
/// You can construct a dictionary by enclosing comma-separated `key: value`
/// pairs in parentheses. The values do not have to be of the same type. Since
/// empty parentheses already yield an empty array, you have to use the special
/// `(:)` syntax to create an empty dictionary.
///
/// A dictionary is conceptually similar to an [array], but it is indexed by
/// strings instead of integers. You can access and create dictionary entries
/// with the `.at()` method. If you know the key statically, you can
/// alternatively use [field access notation]($scripting/#fields) (`.key`) to
/// access the value. To check whether a key is present in the dictionary, use
/// the `in` keyword.
///
/// You can iterate over the pairs in a dictionary using a [for
/// loop]($scripting/#loops). This will iterate in the order the pairs were
/// inserted / declared initially.
///
/// Dictionaries can be added with the `+` operator and [joined together]($scripting/#blocks).
/// They can also be [spread]($arguments/#spreading) into a function call or
/// another dictionary[^1] with the `..spread` operator. In each method, if a
/// key appears multiple times, the last value will override the others.
///
/// [^1]: When spreading into a dictionary, if all items between the parentheses
/// are spread, you have to use the special `(:..spread)` syntax. Otherwise, it
/// will spread into an array.
///
/// # Example
/// ```example
/// #let dict = (
///   name: "Typst",
///   born: 2019,
/// )
///
/// #dict.name \
/// #(dict.launch = 20)
/// #dict.len() \
/// #dict.keys() \
/// #dict.values() \
/// #dict.at("born") \
/// #dict.insert("city", "Berlin")
/// #("name" in dict)
/// ```
#[ty(scope, cast, name = "dictionary")]
#[derive(Default, Clone, PartialEq)]
pub struct Dict(Arc<IndexMap<Str, Value, FxBuildHasher>>);

impl Dict {
    /// Create a new, empty dictionary.

Review comment (suggestion):
Maybe the footnote should be moved to the end of the doc comment? Otherwise, it will appear before the example.

Code after:
pub use crate::__dict as dict;

/// A map from string keys to values.
///
/// You can construct a dictionary by enclosing comma-separated `key: value`
/// pairs in parentheses. The values do not have to be of the same type. Since
/// empty parentheses already yield an empty array, you have to use the special
/// `(:)` syntax to create an empty dictionary.
///
/// A dictionary is conceptually similar to an [array], but it is indexed by
/// strings instead of integers. You can access and create dictionary entries
/// with the `.at()` method. If you know the key statically, you can
/// alternatively use [field access notation]($scripting/#fields) (`.key`) to
/// access the value. To check whether a key is present in the dictionary, use
/// the `in` keyword.
///
/// You can iterate over the pairs in a dictionary using a [for
/// loop]($scripting/#loops). This will iterate in the order the pairs were
/// inserted / declared initially.
///
/// Dictionaries can be added with the `+` operator and [joined together]($scripting/#blocks).
/// They can also be [spread]($arguments/#spreading) into a function call or
/// another dictionary[^1] with the `..spread` operator. In each case, if a
/// key appears multiple times, the last value will override the others.
///
/// # Example
/// ```example
/// #let dict = (
///   name: "Typst",
///   born: 2019,
/// )
///
/// #dict.name \
/// #(dict.launch = 20)
/// #dict.len() \
/// #dict.keys() \
/// #dict.values() \
/// #dict.at("born") \
/// #dict.insert("city", "Berlin")
/// #("name" in dict)
/// ```
///
/// [^1]: When spreading into a dictionary, if all items between the parentheses
/// are spread, you have to use the special `(:..spread)` syntax. Otherwise, it
/// will spread into an array.
#[ty(scope, cast, name = "dictionary")]
#[derive(Default, Clone, PartialEq)]
pub struct Dict(Arc<IndexMap<Str, Value, FxBuildHasher>>);

impl Dict {
    /// Create a new, empty dictionary.