In Rust I use Vec<T> for dynamic arrays. What is the EK9 equivalent?

← Collections and Data Structures · Ref: Q1008

EK9 uses List of T for dynamic collections.

Rust: let mut items: Vec<String> = Vec::new();
EK9: items <- List() of String

Rust: items.push("hello");
EK9: items += "hello"

Rust: items.len()
EK9: items.length()

Rust: for item in &items { println!("{}", item); }
EK9: cat items > stdout

Rust: items.iter().filter(|x| x.len() > 3).collect::<Vec<_>>()
EK9: cat items | filter by isLongEnough | collect as List of String

LIST LITERAL SYNTAX:

Rust: let scores = vec![10, 20, 30];
EK9: scores <- [10, 20, 30]

EK9 lists are always SET when created — even an empty list is set (not unset). This differs from Rust's Option<Vec<T>> pattern.

Example

defines module qa.collections.fromrustvec

  defines function

    isLongEnough() as pure
      -> word as String
      <- rtn as Boolean?

      minimumLength <- 3
      rtn: word.length() > minimumLength

  defines program

    FromRustVecDemo()
      stdout <- Stdout()

      // Like Rust: let mut items = vec!["hello", "world", "hi"]
      items <- ["hello", "world", "hi", "there", "ok"]

      // Like Rust: items.len()
      stdout.println(`Length: ${items.length()}`)

      // Like Rust: for item in &items { println!("{}", item); }
      stdout.println("All items:")
      cat items > stdout

      // Like Rust: items.iter().filter(|x| x.len() > 3).collect()
      longItems <- cat items
        | filter by isLongEnough
        | collect as List of String
      stdout.println(`Long items: ${longItems}`)
Other ways to ask this
  • What is the EK9 equivalent of Rust's Vec?
  • How do I create and use a typed list in EK9?
  • How does EK9's List compare to Rust's Vec?

Coming from another language?

Rust developers: Vec<T> maps to List of T. push() maps to +=. iter().filter().collect() maps to cat | filter | collect.

Keywords: generic, collection, migration, vec, rust, list