Skip to content

Create an empty project:

sh
$ xmake create -l rust -t console test
EXPLORER
src
main.rs
xmake.lua
Lua xmake.lua
12345
add_rules("mode.debug", "mode.release")

target("test")
    set_kind("binary")
    add_files("src/main.rs")

For more examples, see: Rust Examples

Add cargo package dependences

example: https://github.com/xmake-io/xmake/tree/dev/tests/projects/rust/cargo_deps

EXPLORER
src
main.rs
xmake.lua
Lua xmake.lua
123456789
add_rules("mode.release", "mode.debug")

add_requires("cargo::base64 0.13.0")
add_requires("cargo::flate2 1.0.17", {configs = {features = "zlib"}})

target("test")
    set_kind("binary")
    add_files("src/main.rs")
    add_packages("cargo::base64", "cargo::flate2")

Integrating Cargo.toml dependency packages

Integrating dependencies directly using add_requires("cargo::base64 0.13.0") above has a problem.

If there are a lot of dependencies and several dependencies all depend on the same child dependencies, then there will be a redefinition problem, so if we use the full Cargo.toml to manage the dependencies we won't have this problem.

For example

EXPLORER
src
main.rs
Cargo.toml
xmake.lua
Lua xmake.lua
12345678
add_rules("mode.release", "mode.debug")

add_requires("cargo::test", {configs = {cargo_toml = path.join(os.projectdir(), "Cargo.toml")}})

target("test")
    set_kind("binary")
    add_files("src/main.rs")
    add_packages("cargo::test")

For a complete example see: cargo_deps_with_toml

Use cxxbridge to call rust in c++

EXPLORER
src
bridge.rsx
foo.rs
main.cc
xmake.lua
Lua xmake.lua
12345678910111213141516
add_rules("mode.debug", "mode.release")

add_requires("cargo::cxx 1.0")

target("foo")
    set_kind("static")
    add_files("src/foo.rs")
    set_values("rust.cratetype", "staticlib")
    add_packages("cargo::cxx")

target("test")
    set_kind("binary")
    add_rules("rust.cxxbridge")
    add_deps("foo")
    add_files("src/main.cc")
    add_files("src/bridge.rsx")

Call c++ in rust

EXPLORER
src
foo.cc
main.rs
xmake.lua
Lua xmake.lua
12345678910
add_rules("mode.debug", "mode.release")

target("foo")
    set_kind("static")
    add_files("src/foo.cc")

target("test")
    set_kind("binary")
    add_deps("foo")
    add_files("src/main.rs")