Skip to content

Set ​

Native methods and properties ​

MemberSignatureBehavior
sizeusizeNumber of distinct values.
addadd(value: T): Set<T>Inserts a value if it is not already present and returns the set.
hashas(value: T): booleanChecks whether a value is present.
deletedelete(value: T): booleanRemoves a value and reports whether it was present.
valuesvalues(): T[]Returns the current values as an array.

A set can also be traversed directly with for...of. Adding an existing value does not create a duplicate.

tyrus
const uniqueTags: Set<string> = Set(["rust", "native", "rust"]);
uniqueTags.add("fast");                      // Set { "rust", "native", "fast" }
const hasNative: boolean = uniqueTags.has("native"); // true
uniqueTags.delete("fast");                   // Set { "rust", "native" }
const count: usize = uniqueTags.size;        // 2

for (const tag of uniqueTags) {
    print(tag); // "rust", "native"
}