Giter Site home page Giter Site logo

zaksoliman / svelte-rust Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hugo-dz/svelte-rust

0.0 0.0 0.0 360 KB

A simple example to run Rust ๐Ÿฆ€ code in your Svelte application.

Home Page: https://svelte-rust.vercel.app

License: MIT License

JavaScript 42.62% Rust 1.16% TypeScript 1.78% CSS 0.72% HTML 8.52% Svelte 45.20%

svelte-rust's Introduction

Demo Svelte + Rust

๐Ÿ‘‰ You can play around the live demo here

demo

Create a Svelte app

Everything you need to build a Svelte project, powered by create-svelte.

Creating a project

If you're seeing this, you've probably already done this step. Congrats!

# create a new project in the current directory
npm create svelte@latest

# create a new project in my-app
npm create svelte@latest my-app

Developing

Once you've created a project and installed dependencies with npm install (or pnpm install or yarn), start a development server:

npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open

Building

To create a production version of your app:

npm run build

You can preview the production build with npm run preview.

To deploy your app, you may need to install an adapter for your target environment.

Add some Rust ๐Ÿฆ€ to your Svete app

Install Rust by going to the Install Rust page and following the instructions.

Install wasm-pack

cargo install wasm-pack

Create a new Rust package

cargo new --lib my-package

Write a Rust function

// my-package/src/lib.rs

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

Specify the version of the package

By adding crate-type = ["cdylib"]

[package]
name = "my-package"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wasm-bindgen = "0.2.87"

[lib]
crate-type = ["cdylib"]

Build your Rust package

cd my-package
wasm-pack build --target web

Pick the generated pkg folder generated in my-package

And drop it in the /lib folder of your Svelte app

Import your Rust package in your Svelte app

// src/routes/+page.svelte

import init, { add } from "$lib/pkg/my_package";

onMount(async () => {
	await init();
});

function handleClick(): void {
	result = add(numberA, numberB);
}

svelte-rust's People

Contributors

hugo-dz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.