Create an empty project: ```sh $ xmake create -l rust -t console test ``` ```lua [xmake.lua] add_rules("mode.debug", "mode.release") target("test") set_kind("binary") add_files("src/main.rs") ``` For more examples, see: [Rust Examples](https://github.com/xmake-io/xmake/tree/master/tests/projects/rust) ## Add cargo package dependences example: https://github.com/xmake-io/xmake/tree/dev/tests/projects/rust/cargo_deps ```lua 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 ```lua 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](https://github.com/xmake-io/xmake/blob/dev/tests/projects/rust/cargo_deps_with_toml/xmake.lua) ## Use cxxbridge to call rust in c++ example: https://github.com/xmake-io/xmake/tree/dev/tests/projects/rust/cxx_call_rust_library ```lua 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") ``` ```rust [foo.rs] #[cxx::bridge] mod foo { extern "Rust" { fn add(a: i32, b: i32) -> i32; } } pub fn add(a: i32, b: i32) -> i32 { return a + b; } ``` bridge interface file in c++, bridge.rsx ```rust #[cxx::bridge] mod foo { extern "Rust" { fn add(a: i32, b: i32) -> i32; } } ``` ```c++ [main.cc] #include #include "bridge.rs.h" int main(int argc, char** argv) { printf("add(1, 2) == %d\n", add(1, 2)); return 0; } ``` ## Call c++ in rust example: https://github.com/xmake-io/xmake/tree/dev/tests/projects/rust/rust_call_cxx_library ```lua 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") ``` ```rust [main.rs] extern "C" { fn add(a: i32, b: i32) -> i32; } fn main() { unsafe { println!("add(1, 2) = {}", add(1, 2)); } } ``` ```c++ [foo.cc] extern "C" int add(int a, int b) { return a + b; } ```