Skip to content

Instantly share code, notes, and snippets.

@ShaunSHamilton
Created February 28, 2022 23:42
Show Gist options
  • Save ShaunSHamilton/e112fd2d262e065ff2e865a604fda54c to your computer and use it in GitHub Desktop.
Save ShaunSHamilton/e112fd2d262e065ff2e865a604fda54c to your computer and use it in GitHub Desktop.
How to run freeCodeCamp Euler Problems in Rust

How to Run the Project Euler Problems Locally in VSCode

First, you'll need to download and install the freeCodeCamp Courses VSCode extension.

Then, in an empty workspace, open the VSCode command palette with Ctrl/Cmd + Shift + P.

euler-rust-local-ext-1

Select the command freeCodeCamp: Open Course.

euler-rust-local-ext-2

Next, choose the Project Euler: Rust option.

euler-rust-local-ext-3

Once the course is cloned, open the command palette again and select Remote-Containers: Rebuild and Reopen in Container.

euler-rust-local-ext-4

euler-rust-local-ext-5

euler-rust-local-ext-6

Once the container is built, open the command palette again and select freeCodeCamp: Run Course.

euler-rust-local-ext-7

euler-rust-local-ext-8

euler-rust-local-ext-9

How to Run the Project Euler Problems Locally without the Extension

If you would prefer to run these without the VS Code extension, you'll need to fork the repository:

euler-rust-fork

Then clone your fork to your local machine.

git clone https://github.com/<your_username>/euler-rust.git
cd euler-rust

Build and open the Docker container like this:

docker build -f Dockerfile -t euler-rust .

And then start the course:

cd .freeCodeCamp && sample.env .env && npm run dev:curriculum && npm run start

Optionally, you can also clone and build the container with Docker like this:

docker build github.com/<your_username>/euler-rust

How to Run the Project Euler Problems Using Gitpod

GitPod is a popular tool for running a VM in your browser, and is yet another way you can solve these Project Euler problems. First, fork the repository:

euler-rust-fork

Then open your fork in Gitpod: https://gitpod.io/#https://github.com/<your_user_name>/euler-rust

euler-rust-gitpod-button

euler-rust-gitpod-setup

Once the setup is complete, open the command pallete with Ctrl/Cmd + Shift + P:

euler-rust-gitpod-setup-done

Select the command freeCodeCamp: Run Course.

euler-rust-gitpod-run-course

Useful Information

NOTE: If you are using any of the above methods with Docker, you will need to have Docker installed on your machine, and have the Daemon running.

You should need to only edit the src/lib.rs file, and you can follow the example code there to get started.

To compile your code, before running the tests, run:

npm run build

If at any point you get stuck, I recommend that you check out more information about Rust with WASM. Otherwise, feel free to open a new topic on the freeCodeCamp forum.

How the Project Works

First, taking your Rust code in src/lib.rs:

use wasm_bindgen::prelude::*;

// Example format to write functions:
#[wasm_bindgen(js_name = camelCaseName)] // js_name must equal function name tested on client
pub fn snake_case_name(num: i32) -> i32 { // Function must be public
    // All numbers in JS are 32-bit
    num
}

The Rust is transpiled into JavaScript code using wasm-pack:

import * as wasm from "./curriculum_bg.wasm";

/**
 * @param {number} n
 * @returns {number}
 */
export function camelCaseName(num) {
  var ret = wasm.camelCaseName(num);
  return ret;
}

Then, Webpack again transpiles the JavaScript into an ES5-valid bundle, because the tests are run within Node.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment