Skip to content

Commit b53f965

Browse files
bn3tDeMoorJasper
authored andcommitted
Add rust example based on rust cargo compilation (#41)
1 parent 1d2eb4c commit b53f965

File tree

12 files changed

+152
-0
lines changed

12 files changed

+152
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ npm-debug.log*
55
yarn-debug.log*
66
yarn-error.log*
77
yarn.lock*
8+
package-lock.json
9+
Cargo.lock
810

911
# Runtime data
1012
pids

rust-cargo/add/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "add"
3+
version = "0.1.0"
4+
authors = []
5+
6+
[dependencies]
7+
8+
[lib]
9+
crate-type = ["cdylib"]

rust-cargo/add/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[no_mangle]
2+
pub fn add(a: i32, b: i32) -> i32 {
3+
return a + b
4+
}
5+
6+
#[cfg(test)]
7+
mod tests {
8+
use super::*;
9+
10+
#[test]
11+
fn simple_add() {
12+
assert_eq!(add(5, 4), 9);
13+
}
14+
}

rust-cargo/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "parcel-rustc-example",
3+
"description": "Simple Rustc example",
4+
"version": "1.0.0",
5+
"main": "src/index.html",
6+
"license": "MIT",
7+
"scripts": {
8+
"start": "parcel src/index.html --open",
9+
"build": "parcel build src/index.html"
10+
},
11+
"dependencies": {},
12+
"devDependencies": {
13+
"cssnano": "^4.1.7",
14+
"parcel-bundler": "^1.10.3"
15+
}
16+
}

rust-cargo/src/App.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
html,
2+
body {
3+
width: 100%;
4+
height: 100%;
5+
padding: 0;
6+
margin: 0;
7+
}
8+
9+
body {
10+
background: white;
11+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji',
12+
'Segoe UI Emoji', 'Segoe UI Symbol';
13+
}
14+
15+
div {
16+
display: flex;
17+
justify-content: center;
18+
align-items: center;
19+
width: 100%;
20+
height: 100%;
21+
}
22+
23+
h1 {
24+
font-weight: 300;
25+
}

rust-cargo/src/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>Parcel Rust Cargo Example</title>
6+
<meta charset="UTF-8" />
7+
</head>
8+
9+
<body>
10+
<div>
11+
<h1>Hello World from Rust Cargo! 📦 🚀 -- add(4, 5) gives <span id="result"></span></h1>
12+
</div>
13+
14+
<script src="./main.js"></script>
15+
</body>
16+
17+
</html>

rust-cargo/src/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './App.css'
2+
import { add } from '../add/src/lib.rs'
3+
4+
document.getElementById("result").innerText = add(5, 4);

rustc/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "parcel-rustc-example",
3+
"description": "Simple Rust Cargo example",
4+
"version": "1.0.0",
5+
"main": "src/index.html",
6+
"license": "MIT",
7+
"scripts": {
8+
"start": "parcel src/index.html --open",
9+
"build": "parcel build src/index.html"
10+
},
11+
"dependencies": {},
12+
"devDependencies": {
13+
"parcel-bundler": "^1.10.3"
14+
}
15+
}

rustc/src/App.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
html,
2+
body {
3+
width: 100%;
4+
height: 100%;
5+
padding: 0;
6+
margin: 0;
7+
}
8+
9+
body {
10+
background: white;
11+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji',
12+
'Segoe UI Emoji', 'Segoe UI Symbol';
13+
}
14+
15+
div {
16+
display: flex;
17+
justify-content: center;
18+
align-items: center;
19+
width: 100%;
20+
height: 100%;
21+
}
22+
23+
h1 {
24+
font-weight: 300;
25+
}

rustc/src/add.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[no_mangle]
2+
pub fn add(a: i32, b: i32) -> i32 {
3+
a + b
4+
}

0 commit comments

Comments
 (0)