This is the latest docs version
Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
Language Manual
Overview
  • Introduction
  • Installation
  • Migrate to v11
  • Editor Plugins
  • Try
Language Features
  • Overview
  • Let Binding
  • Type
  • Primitive Types
  • Tuple
  • Record
  • Object
  • Variant
  • Polymorphic Variant
  • Null, Undefined and Option
  • Array & List
    • Array
    • List
  • Function
  • If-Else & Loops
  • Pipe
  • Pattern Matching / Destructuring
  • Mutation
  • JSX
  • Exception
  • Lazy Value
  • Promises
  • Async / Await
  • Tagged templates
  • Module
  • Import & Export
  • Attribute (Decorator)
  • Reserved Keywords
  • Equality and Comparison
Advanced Features
  • Extensible Variant
  • Scoped Polymorphic Types
JavaScript Interop
  • Interop Cheatsheet
  • Embed Raw JavaScript
  • Shared Data Types
  • External (Bind to Any JS Library)
  • Bind to JS Object
  • Bind to JS Function
  • Import from / Export to JS
  • Bind to Global JS Values
  • JSON
  • Inlining Constants
  • Use Illegal Identifier Names
  • Generate Converters & Helpers
  • Browser Support & Polyfills
  • Libraries & Publishing
  • TypeScript
Build System
  • Overview
  • Configuration
  • Configuration Schema
  • External Stdlib
  • Pinned Dependencies
  • Interop with JS Build Systems
  • Performance
  • Warning Numbers
Guides
  • Converting from JS
Extra
  • Newcomer Examples
  • Project Structure
  • FAQ
Docs / Language Manual / Array & List
Edit

Array and List

Array

Arrays are our main ordered data structure. They work the same way as JavaScript arrays: they can be randomly accessed, dynamically resized, updated, etc.

ReScriptJS Output
let myArray = ["hello", "world", "how are you"]

ReScript arrays' items must have the same type, i.e. homogeneous.

Usage

Access

Accessing items in an array will return an option and can be done like so:

ReScriptJS Output
let myArray = ["hello", "world", "how are you"]

let firstItem = myArray[0] // Some("hello")

let tenthItem = myArray->Array.get(10) // None

The behavior of returning an option is new to V11 when you have Core open. It provides a safer way to access array items, which is especially useful when you're not sure if the index is out of bounds. If you would like to not use an option, you can use Array.getUnsafe.

Update

Items in an array can be updated by assigning a value to an index or using a function:

ReScriptJS Output
let myArray = ["hello", "world", "how are you"]

myArray[0] = "hey" // now ["hey", "world", "how are you"]

myArray->Array.push("?") //  ["hey", "world", "how are you", "?"]

myArray->Array.set(0, "bye") //  ["bye", "world", "how are you", "?"]

Array spreads

Since 11.1

You can spread arrays of the the same type into new arrays, just like in JavaScript:

ReScriptJS Output
let y = [1, 2]
let x = [4, 5, ...y]
let x2 = [4, 5, ...y, 7, ...y]
let x3 = [...y]

Note that array spreads compiles to Belt.Array.concatMany right now. This is likely to change to native array spreads in the future.

List

ReScript provides a singly linked list too. Lists are:

  • immutable

  • fast at prepending items

  • fast at getting the head

  • slow at everything else

ReScriptJS Output
let myList = list{1, 2, 3}

Like arrays, lists' items need to be of the same type.

Usage

You'd use list for its resizability, its fast prepend (adding at the head), and its fast split, all of which are immutable and relatively efficient.

Do not use list if you need to randomly access an item or insert at non-head position. Your code would end up obtuse and/or slow.

The standard lib provides a List module.

Immutable Prepend

Use the spread syntax:

ReScriptJS Output
let myList = list{1, 2, 3}
let anotherList = list{0, ...myList}

myList didn't mutate. anotherList is now list{0, 1, 2, 3}. This is efficient (constant time, not linear). anotherList's last 3 elements are shared with myList!

Note that list{a, ...b, ...c} was a syntax error before compiler v10.1. In general, the pattern should be used with care as its performance and allocation overhead are linear (O(n)).

Access

switch (described in the pattern matching section) is usually used to access list items:

ReScriptJS Output
let message =
  switch myList {
  | list{} => "This list is empty"
  | list{a, ...rest} => "The head of the list is the string " ++ Int.toString(a)
  }
Null, Undefined and OptionFunction

© 2024 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on