Member-only story
Indexing Strings in Rust and TypeScript: A Case Study of Strings
How Rust and JavaScript process strings and handle the nuances such as grapheme, or even emojis.

TL;DR
- 🪢 Accessing characters in strings with index is not compilable in Rust.
- 🎸 We’ll discuss how Rust think about strings.
- 🤖 We’ll discuss how JavaScript handles strings.
- 🧑🔬 We’ll compare a classic algorithm
is_palindrome
in Rust and TypeScript.
Text is essential in programming languages. String is the Rust’s and JavaScript’s definition to process text in written languages around the world. Through the simple concept of string indexing, we’ll discuss how Rust and JavaScript process strings and how they handle the nuances in strings such as grapheme, or even emojis.
Let’s use a classic algorithm is_palindrome
to introduce the concept of string indexing.
is_palindrome
in Rust
A palindrome, in a very general way of explaining it, is a string that reads the same forward and backward. “Ana” is a palindrome, “A dog! A panic in a pagoda!” is a palindrome, or even “02/02/2020” is a palindrome.
For the purpose of this article, we’ll use a more narrowed definition to keep the algorithm simple. A palindrome here is defined as a “continuous sequence of lowercase ASCII alphabetical characters without whitespace”.
One intuitive approach would be using two pointers. One starts from the beginning of the given string moving toward the end, and the other from the end toward the beginning. While moving the pointers, compare the pointing characters. The given string is a palindrome if all the comparisons are equal. Like this:
If you try to compile the program, you’ll notice that the Rust compiler won’t let us access characters by index. It’s a very interesting constraint because many languages like JavaScript, Go…