小编aer*_*gnr的帖子

使用Elixir通过Ports与Rust交谈,我怎么了?

我正在编写教程,因为在任何地方都找不到通过端口在Elixir和Rust之间进行通信的简单示例。

我可以让Rustler工作,但这是NIF,而不是端口。

我的代码中缺少基本的东西。我不确定我是否在stdio中缺少基本的东西,或者是否还有其他东西,但是我尝试了很多不同的东西。

可以使用Rust中的一个非常基本的程序进行端口通信:

use std::env;

fn main() {
    println!("hello world!");
}
Run Code Online (Sandbox Code Playgroud)

我可以通过运行以下端口来将其加入我的iex -S组合中:

defmodule PortExample do

  def test() do
    port = Port.open({:spawn_executable, "_build/dev/rustler_crates/portexample/debug/portexample"}, [:binary])
    Port.info(port)
    port
end
Run Code Online (Sandbox Code Playgroud)

这是该iex的样子:

defmodule PortExample do

  def test() do
    port = Port.open({:spawn_executable, "_build/dev/rustler_crates/portexample/debug/portexample"}, [:binary])
    Port.info(port)
    port
end
Run Code Online (Sandbox Code Playgroud)

我可以使用瓷器库调用执行相同的操作:

  alias Porcelain.Result
  def porcelain() do
    result = Porcelain.exec("_build/dev/rustler_crates/portexample/debug/portexample",["hello", "world"])
    IO.inspect result.out
  end
Run Code Online (Sandbox Code Playgroud)

对应的IEX:

Interactive Elixir (1.4.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> PortExample.test()
#Port<0.9420>
iex(2)> flush()
{#Port<0.9420>, …
Run Code Online (Sandbox Code Playgroud)

port native elixir rust

3
推荐指数
1
解决办法
469
查看次数

标签 统计

elixir ×1

native ×1

port ×1

rust ×1