メインコンテンツまでスキップ
Version: 0.21

Immutable Types

What are immutable types?

These are types that you can instantiate but never mutate the values. In order to update a value, you must instantiate a new value.

Why using immutable types?

Properties, like in React, are propagated from ancestors to children. This means that the properties must live when each component is updated. This is why properties should —ideally— be cheap to clone. To achieve this we usually wrap things in Rc.

Immutable types are a great fit for holding property's values because they can be cheaply cloned when passed from component to component.

Common Immutable Types

Yew recommends using the following immutable types from the implicit-clone crate:

  • IString (aliased as AttrValue in Yew) - for strings instead of String
  • IArray<T> - for arrays/vectors instead of Vec<T>
  • IMap<K, V> - for maps instead of HashMap<K, V>

These types are either reference-counted (Rc) or static references, making them very cheap to clone.

Further reading