🎓 My journey from JavaScript to Rust

Related tags

Utilities rusty-days
Overview

Rusty Days

My journey from JavaScript to Rust

Rust (aka RustLang) is a language for performance, reliability, and productivity source

Resources Used:

Cheat-Sheet

YouTube

Books



Projects Completed



Variables

  • The two options are let and const
  • Unlike JavaScript, both are immutable in Rust.

let

  • The most common
  • Write as snake_case

const

  • Requires explicit typing at initialization const a:char = 'a'
  • Cannot be marked mut
  • Write as SCREAMING_SNAKE_CASE

Types

Array-like

If length is dynamic, you must use vector, otherwise arrays and tuples are more optimized

Vectors

The most flexible option

let mut v = vec![0];
println!("element at index {} is {}", 0, vec[0]);

Arrays

Fixed length at compile-time - Like Tuples except all elements must be of same type

let a: [i32; 5] = [1, 2, 3, 4, 5];


let months = ["January", "February", "March", "April", "May", "June", "July",
              "August", "September", "October", "November", "December"];

// these two are the same
let a = [3, 3, 3, 3, 3];
let a = [3; 5];

let first = a[0];
let second = a[1];

Tuples

Fixed Length at compile-time - Not just two elements like some langauges do

  • Accessing tuple values is done with .
let long_tuple = (1u8, 2u16, 3u32, 4u64,
                      -1i8, -2i16, -3i32, -4i64,
                      0.1f32, 0.2f64,
                      'a', true);

// Values can be extracted from the tuple using tuple indexing
println!("long tuple first value: {}", long_tuple.0);
println!("long tuple second value: {}", long_tuple.1);
  • Long tuples cannot be printed
// long Tuples cannot be printed
// let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
// println!("too long tuple: {:?}", too_long_tuple);
// TODO ^ Uncomment the above 2 lines to see the compiler error

Functions

  • Functions do not need to say return
fn reverse(pair: (i32, bool)) -> (bool, i32) {
    // `let` can be used to bind the members of a tuple to variables
    let (integer, boolean) = pair;

    (boolean, integer)
}

String like

We have chars and two types of strings

Char

Wrapped in '. Length 1

const A:char = 'a';

Primitive String

Immutable and fixed-length

let hello = "hello";

String

Wrapped in ". Growable

let mut hello = String::from("hello");
hello.push('W');
hello.push_str("orld!");

Etc.

Semicolons mean a lot

Everything in rust is either a statement or an expression

  • Statements do not return values
fn main() {
    // this is a statement
    let a = 1;

    // this will error
    // the statement `let y = 6` does not return anything
    let x = (let y = 6);
}
  • This program is valid. Add a semi to x+1 and not-so!
fn main() {
    let y = {
        let x = 3;
        // notice the lack of `;`
        // this is a valid expression
        x + 1
    };

    println!("The value of y is: {}", y);
}
You might also like...
Migrate C code to Rust
Migrate C code to Rust

C2Rust helps you migrate C99-compliant code to Rust. The translator (or transpiler) produces unsafe Rust code that closely mirrors the input C code. T

C to Rust translator

Corrode: Automatic semantics-preserving translation from C to Rust This program reads a C source file and prints an equivalent module in Rust syntax.

Astronomical algorithms in Rust

astro-rust Contents API Docs About Usage Contributing References About astro-rust is a library of advanced astronomical algorithms for the Rust progra

A Rust library for calculating sun positions

sun A rust port of the JS library suncalc. Install Add the following to your Cargo.toml [dependencies] sun = "0.2" Usage pub fn main() { let unixti

Macro for Python-esque comprehensions in Rust

Cute Macro for Python-esque list comprehensions in Rust. The c! macro implements list and hashmap comprehensions similar to those found in Python, all

Language Integrated Query in Rust.

Linq in Rust Language Integrated Query in Rust (created by declarative macros). Inspired by LINQ in .NET. What's LINQ This project is under developmen

A cross-platform serial port library in Rust.

Introduction serialport-rs is a general-purpose cross-platform serial port library for Rust. It provides a blocking I/O interface and port enumeration

A Rust macro for writing regex pattern matching.

regexm A Rust macro for writing regex pattern matching.

Simple ray tracer written in Rust
Simple ray tracer written in Rust

Simple ray tracer written in Rust from scratch I've just finished my first semester at the Faculty of Applied Mathematics and Computer Science at the

Owner
Dawson Botsford
Founder of earni.fi 🚁 Web3 + Ethereum Builder. Helping you find airdrops and stay safe in DeFi. 🦇 🔊
Dawson Botsford
Blazing fast linter for JavaScript and TypeScript written in Rust

deno_lint A Rust crate for writing fast JavaScript and TypeScript linters. This crate powers deno lint, but is not Deno specific and can be used to wr

Deno Land 1.3k Dec 29, 2022
Extremely fast JavaScript minifier, available for Rust and Node.js

minify-js Extremely fast JavaScript minifier, written in Rust. Goals Fully written in Rust for maximum compatibility with Rust programs and derivative

Wilson Lin 78 Jul 13, 2023
Adapter plugin to use Ruff in dprint's CLI and with JavaScript via Wasm

dprint-plugin-ruff Adapter for Ruff for use as a formatting plugin in dprint. Formats .py and .pyi files. Note: For formatting .ipynb files, use the J

null 3 Nov 28, 2023
k-mer counter in Rust using the rust-bio and rayon crates

krust is a k-mer counter written in Rust and run from the command line that will output canonical k-mers and their frequency across the records in a f

null 14 Jan 7, 2023
Experimental Rust tool for generating FFI definitions allowing many other languages to call Rust code

Diplomat is an experimental Rust tool for generating FFI definitions allowing many other languages to call Rust code. With Diplomat, you can simply define Rust APIs to be exposed over FFI and get high-level C, C++, and JavaScript bindings automatically!

null 255 Dec 30, 2022
Aws-sdk-rust - AWS SDK for the Rust Programming Language

The AWS SDK for Rust This repo contains the new AWS SDK for Rust (the SDK) and its public roadmap. Please Note: The SDK is currently released as a dev

Amazon Web Services - Labs 2k Jan 3, 2023
Rust + Yew + Axum + Tauri, full-stack Rust development for Desktop apps.

rust-yew-axum-tauri-desktop template Rust + Yew + Axum + Tauri, full-stack Rust development for Desktop apps. Crates frontend: Yew frontend app for de

Jet Li 54 Dec 23, 2022
A lightning fast version of tmux-fingers written in Rust, copy/pasting tmux like vimium/vimperator

tmux-thumbs A lightning fast version of tmux-fingers written in Rust for copy pasting with vimium/vimperator like hints. Usage Press ( prefix + Space

Ferran Basora 598 Jan 2, 2023
A command-line tool collection to assist development written in RUST

dtool dtool is a command-line tool collection to assist development Table of Contents Description Usage Tips Installation Description Now dtool suppor

GB 314 Dec 18, 2022
Rust mid-level IR Abstract Interpreter

MIRAI MIRAI is an abstract interpreter for the Rust compiler's mid-level intermediate representation (MIR). It is intended to become a widely used sta

Facebook Experimental 793 Jan 2, 2023