Skip to content

Commit c8a198c

Browse files
committed
Merge pull request alexcrichton#9 from billpmurphy/master
add Haskell to Rust example
2 parents 9b6be2f + a076bb9 commit c8a198c

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

haskell-to-rust/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "haskell-to-rust"
3+
version = "0.1.0"
4+
authors = ["Bill Murphy <[email protected]>"]
5+
6+
[lib]
7+
name = "double_input"
8+
crate-type = ["dylib"]

haskell-to-rust/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ifeq ($(shell uname),Darwin)
2+
EXT := dylib
3+
else
4+
EXT := so
5+
endif
6+
7+
all: target/debug/libdouble_input.$(EXT)
8+
ghc src/Main.hs target/debug/libdouble_input.$(EXT) -o main
9+
10+
target/debug/libdouble_input.$(EXT): src/lib.rs Cargo.toml
11+
cargo build
12+
13+
clean:
14+
rm -rf target
15+
rm main
16+
rm src/*.o src/*.hi

haskell-to-rust/src/Main.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{-# LANGUAGE ForeignFunctionInterface #-}
2+
3+
import Foreign.C
4+
5+
foreign import ccall "double_input" doubleInput :: CInt -> CInt
6+
7+
main = do
8+
let input = 4
9+
let output = doubleInput input
10+
putStrLn $ show input ++ " * 2 = " ++ show output

haskell-to-rust/src/lib.rs

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

0 commit comments

Comments
 (0)