Closed stellarpower closed 10 months ago
The examples like example/fibonacci.rs
can be run like cargo run --example fibonacci
.
The examples like examples/rust_blank
can be run like:
cd examples/rust_blank
rake test
For an example of a complete gem, see https://github.com/matsadler/halton-rb
The tests can be run with rake test
.
That can be built into a .gem
file with gem build halton.gemspec
and then installed with gem install halton-0.3.0.gem
(or whatever version was built). Once installed you could use it like:
> irb
irb(main):001:0> require "halton"
=> true
irb(main):002:0> Halton.number(2, 1)
=> 0.5
That will build a gem that contains the Rust source, and then compiles the Rust code when it is installed. To instead compile the Rust code when the gem is built run rake native gem
, and you will find the .gem
file is named something like halton-0.3.0-arm64-darwin-22.gem
. In this example the gem would only work on ARM (Apple Silicon, i.e. M1, M2, etc) Macs, because that's where I compiled it.
It's possible to cross-compile a gem to contain pre-compiled code for multiple operating systems / CPU architectures, but that's time consuming to get working (or in my case remember how it works), I'd recommend using the oxidize-rb/actions/cross-gem GitHub action in that case.
Hi,
thanks for the examples.
I followed your advise and cloned the halton-repository, executed bundle install
and bundle exec rake compile
. Everything worked well.
Except an annoying warning.
Every time I start a console and require 'halton' this is displayed
Ignoring halton-0.3.0 because its extensions are not build.
The rust extension is loaded correctly. Whats the reason and how to get rid of that message?
You didn't give an example of the exact situation you're experiencing the warning, but there is a bug somewhere in RubyGems and/or Bundler that causes that message to appear for any native extension gem (Rust or C) that is in the Gemfile as a path dependency, e.g. gem "foo", path: "/path/to/foo"
(the gemspec
line in a Gemfile will also create a path dependency to the current directory), so I'm guessing it's that.
I'm having some troubles building the examples - getting some weird errors out of Cargo. Am not a Rust user myself, and whilst confident with Ruby, never really had to use rake or bundler or package a gem before.
Please could you provide a copy-pastable example of how to build one of the examples in the example folder, so that it's possible to verify that I/someone else is doing everything corectly?
Thanks