Rust for typescript devs part 3 - Strings

Rhl - Sep 1 - - Dev Community

Hi, I am Rahul and I learnt rust to build Loadjitsu.io. This is the third post in my series on "Rust for typescript devs".
Read the second part about variables and mutability here.
Read the intro post here.

Strings are a fundamental data type in any programming language, allowing you to work with text data. As a TypeScript developer, you're accustomed to using strings in a straightforward manner, with the string type being a key part of the language. In Rust, strings are slightly more complex, offering both string slices and owned strings, each with its specific use cases. In this section, we’ll explore how Rust handles strings and how these concepts relate to what you already know from TypeScript.

String Types in TypeScript and Rust

TypeScript:

In TypeScript, strings are simple and flexible. They are always of the string type, and you can create them using either single or double quotes, or template literals.

let greeting: string = "Hello, world!";
let name: string = `TypeScript Developer`;

Enter fullscreen mode Exit fullscreen mode

Rust:

Rust offers two main types for handling strings: &str (string slices) and String. Each serves a different purpose, providing more control over how memory is managed.

  • &str (String Slice): This is a reference to a sequence of characters. It’s immutable and does not own the data it points to, similar to a borrowed reference.
  • String: This is an owned, growable string. It’s mutable and heap-allocated, which means it can be resized and modified.
let greeting: &str = "Hello, world!"; // Immutable, similar to a 'const' string in TypeScript
let mut name: String = String::from("Rust Developer"); // Mutable, similar to a 'let' string in TypeScript

Enter fullscreen mode Exit fullscreen mode

Key Difference: Unlike TypeScript’s string, which is a catch-all type, Rust differentiates between string slices (&str) and owned strings (String). This distinction allows for more efficient memory management and control over how strings are used and modified.

Creating and Modifying Strings

TypeScript:

In TypeScript, strings are immutable. Once created, you cannot change the string itself, but you can create a new string based on the existing one.

let greeting: string = "Hello";
let name: string = "TypeScript Developer";
let message: string = `${greeting}, ${name}!`; // String interpolation

Enter fullscreen mode Exit fullscreen mode

Rust:

Rust strings can be modified, but you must use the String type if you want to change the contents of the string.

let greeting: &str = "Hello";
let mut name: String = String::from("Rust Developer");
name.push_str(" is awesome!"); // Modifying the String
let message = format!("{}, {}!", greeting, name); // String formatting, similar to interpolation

Enter fullscreen mode Exit fullscreen mode

Similarity: Both TypeScript and Rust allow you to create new strings based on existing ones. However, Rust requires you to use the String type if you want to modify the string after it’s created, whereas TypeScript strings remain immutable.

String Slices vs. Owned Strings

Understanding the difference between &str and String is crucial when working with Rust. Here’s how they compare:

  • String Slices (&str): These are similar to references to strings in TypeScript. They’re lightweight, do not own the data, and are generally more efficient for read-only operations.

    
    fn greet(name: &str) {
        println!("Hello, {}!", name);
    }
    
    let name = "Rust Developer";
    greet(name); // Passing a string slice to a function
    
    
  • Owned Strings (String): These are mutable and own the data they contain. If you need to modify or own the string, you’ll use String.

    
    let mut name = String::from("Rust Developer");
    name.push_str(" is learning Rust!"); // Modifying the string
    
    

Similarity: In TypeScript, you often pass strings as references without worrying about ownership or mutability. In Rust, you need to be mindful of whether you’re working with a borrowed reference (&str) or an owned, mutable string (String). This distinction is a key part of Rust’s ownership system, which helps prevent memory-related bugs.

Concatenation and Interpolation

Both TypeScript and Rust allow you to concatenate strings and insert variables within strings, but the syntax differs slightly.

TypeScript:


let greeting = "Hello";
let name = "TypeScript Developer";
let message = greeting + ", " + name + "!"; // Concatenation

let interpolatedMessage = `${greeting}, ${name}!`; // Interpolation

Enter fullscreen mode Exit fullscreen mode

Rust:


let greeting = "Hello";
let name = "Rust Developer";
let message = [greeting, ", ", name, "!"].concat(); // Concatenation using an array of string slices

let interpolated_message = format!("{}, {}!", greeting, name); // Interpolation using format!

Enter fullscreen mode Exit fullscreen mode

Key Difference: While TypeScript uses the + operator for concatenation and template literals for interpolation, Rust provides the format! macro for interpolation and typically uses other methods like push_str or array concatenation for joining strings.

Continue to the next post about Functions in rust here

. . . . .