转载: Rust for JavaScript Developers - Variables and Data Types
This is the second part in a series about introducing the Rust language to JavaScript developers. Here are all the chapters:
- Tooling Ecosystem Overview
- Variables and Data Types
- Functions and Control Flow
- Pattern Matching and Enums
# Variables
JavaScript has three ways to declare variables - var
, const
and let
. Rust also has const
and let
but they work very differently compared to JavaScript.
# let
In Rust, we declare variables using let
.
1 | let a = 123; |
As expected, this assigns the value 123
to the variable a
. But Rust by default makes the variable immutable. You won’t be able to change the value after it’s assigned.
1 | let a = 123; |
At first glance, this might look similar to JavaScript’s const but JavaScript’s const doesn’t make variables immutable, it just makes the binding immutable.
If you want to make variable mutable, you need to explicitly mention it using the mut
keyword
1 | let mut a = 123; |
# const
Rust’s const
is also very different from JavaScript’s const - it’s better to think of Rust’s const as a “label” to a constant value. During compile time they get replaced by their actual values in all the places they are used. It’s usually used for constants like port numbers, timeout values, error codes etc.
You can also define const outside functions at global level which can’t be done using let.
# Data Types
# Numbers
In JavaScript, we’ve the Number type for both integers (numbers without decimal point) and floats (numbers with decimal point). In Rust, there’s a ton of options for integers and floats but by default we can use i32
for integers and f64
for floats.
1 | let x = 123; // i32 |
# Booleans
Pretty straightforward - both JavaScript and Rust have booleans with true/false values
1 | let x = false; // bool |
# Strings
We usually don’t think much about strings when working with JavaScript - they “just work”. In Rust, there are many types of strings but let’s focus on the widely used ones - String
and &str
.
String
is growable whereas &str
is immutable and fixed size.
When you create a string using a string literal, it creates a &str
type:
1 | let name = "Saitama"; // &str |
You need to use String::from
or to_string
methods to create a String
type:
1 | let name = String::from("Genos"); // String |
You can convert from String
to &str
using the as_str
function
1 | let name2 = "King".to_string(); // String |
We’ll learn more about strings in future posts.
# Optionals
JavaScript has two types for empty values - undefined
and null
. Undefined is used when a variable, property etc is not defined and null is used when something is intentionally empty.
Rust has neither of these - it doesn’t even have a dedicated null data type. Instead it has something call Option
. When we have a situation where a value can be empty or initially undefined, this Option
type is used.
People who have worked with TypeScript/Flow might see some similarities here but it’s quite different in terms of how the optionals are created and used.
Say we want to write a function that takes in a file path and returns its contents. Let’s say for whatever reason, we want to return a “null” value when empty string is passed as file path.
Here’s how we would write this in JavaScript/TypeScript:
1 | function read_file(path: string): string | null { |
Implementing the same using Rust’s Option
:
1 | fn read_file(path: &str) -> Option<&str> { |
You can see that we return None
for null value but for non-null value, we don’t return the contents
as it is, but rather we “wrap” it inside Some
and return that. The return type is also not “string or null” as per the TypeScript example but “Option that contains &str” type.
Here’s how you would call this function in JavaScript/TypeScript:
1 | function main() { |
Calling the function in Rust:
1 | fn main() { |
As you can see, we need to manually “unwrap” the Option to get the contents inside.
# Arrays
Similar to strings, there are two types of arrays - one with fixed size (simply referred to as “Array”) and other that can grow/shrink in size (called “Vectors”).
Arrays:
1 | fn main() { |
Vectors:
1 | fn main() { |
# Objects
Technically, all non-primitive types are “objects” in JavaScript but we commonly use the term “object” for two things - bag of data or hash map.
Bag of data:
Unlike other languages, you need not go through a lot of ceremony to create an object and this is one of the coolest things about JavaScript.
To create an employee object in JavaScript:
1 | function main() { |
To create the same object in Rust, we can use structs:
1 | struct Employee { |
HashMap:
In JavaScript, to create an object with arbitrary key value pairs, we can either use normal object literals or the Map object:
1 | function main() { |
In Rust, you can do the same using the HashMap type:
1 | use std::collections::HashMap; |
Notice the usage of unwrap above. HashMap’s get method returns an Option type which we need to unwrap to get the value inside.