rust-cargo

How can I build multiple binaries with Cargo?

跟風遠走 提交于 2019-11-28 06:10:20
I'd like to make a project with a daemon and a client , connecting through a unix socket. A client and a daemon requires two binaries, so how do I tell Cargo to build two targets from two different sources? To add a bit of fantasy, I'd like to have a library for the main part of the daemon , and just have a binary to wrap around it and communicate through sockets. So, we have this kind of tree architecture: ├── Cargo.toml ├── target | └── debug | ├── daemon │ └── client └── src ├── daemon │ ├── bin │ │ └── main.rs │ └── lib │ └── lib.rs └── client └── bin └── main.rs I could make one

Can tests be built in release mode using Cargo?

五迷三道 提交于 2019-11-28 02:53:06
问题 I'm using cargo build --release to build my project in release configuration and cargo test to build and run my tests. However, I'd like to also build my tests in release mode; can this be done using cargo? 回答1: cargo test --release exists, but it is slightly different than just enabling optimizations. For example, debug assertions become disabled. You can also set opt-level in the [profile.test] section of your Cargo.toml, as Viktor Dahl suggests. 来源: https://stackoverflow.com/questions

Can Cargo download and build dependencies without also building the application?

若如初见. 提交于 2019-11-28 02:36:27
问题 Is there a way to tell Cargo to install and build all my dependencies, but not attempt to build my application? I thought cargo install would do that, but it actually goes all the way to building my app too. I want to get to a state where cargo build would find all dependencies ready to use, but without touching the /src directory. What I'm really trying to accomplish: I'm trying to build a Docker image for a Rust application, where I'd like to do the following steps: Build time ( docker

What is an idiomatic way to have shared utility functions for integration tests and benchmarks?

删除回忆录丶 提交于 2019-11-28 01:30:30
I have Rust project with both integration tests (in the /tests dir) and benchmarks (in the /benches dir). There are a couple of utility functions that I need in tests and benches, but they aren't related to my crate itself, so I can't just put them in the /utils dir. What is idiomatic way to handle this situation? Create a shared crate (preferred) As stated in the comments, create a new crate. You don't have to publish the crate to crates.io . Just keep it as a local unpublished crate inside your project and mark it as a development-only dependency : . ├── Cargo.toml ├── src │ └── lib.rs ├──

How do I specify the linker path in Rust?

可紊 提交于 2019-11-27 23:07:59
I'm trying to link a Rust program with libsoundio . I'm using Windows and there's a GCC binary download available. I can link it like this if I put it in the same folder as my project: #[link(name = ":libsoundio-1.1.0/i686/libsoundio.a")] #[link(name = "ole32")] extern { fn soundio_version_string() -> *const c_char; } But I really want to specify #[link(name = "libsoundio")] or even #[link(name = "soundio")] , and then provide a linker path somewhere else. Where can I specify that path? I tried the rustc-link-search suggestion as follows: #[link(name = "libsoundio")] #[link(name = "ole32")]

How do I use conditional compilation with `cfg` and Cargo?

柔情痞子 提交于 2019-11-27 23:07:28
问题 I want to conditionally compile my source code using cfg with Cargo, after Googling for a while, it seems that the solution is to use cargo --features . http://doc.crates.io/manifest.html I tried adding a few #[cfg(feature = "foo")] in the source code and cargo build --features foo , but it says Package `xxx v0.0.1 (file:///C:/yyy/xxx)` does not have these features: `foo` How can I let cargo identify the features? Do I have to add something in Cargo.toml ? Here's the version of rustc and

Linking Rust application with a dynamic library not in the runtime linker search path

大憨熊 提交于 2019-11-27 20:54:58
I have a shared library that I'd like to dynamically link into several separate binary Cargo applications. I include its location in the linker using the -- -L /path/to/dir format and the application compiles correctly with the significant decrease in binary size I expect. However, when checking the generated binary using ldd , I get a message saying that the library couldn't be found: casey@Gilthar-II:~/bot4/backtester/target/release$ ldd backtester linux-vdso.so.1 => (0x00007ffc642f7000) libalgobot_util.so => not found If I add the library to the /lib/x86_64-linux-gnu directory, the

Is there a list of all cfg features?

偶尔善良 提交于 2019-11-27 19:09:54
Rust has the ability to check configuration at build time with, e.g., #[cfg(target_os = "linux")] or if cfg!(target_os = "linux") {...} , where target_os is a feature . Is there a list of all (or, at least, commonly used) features that can be checked in Rust? See related question regarding attributes Is there an exhaustive list of standard attributes anywhere? . Wesley Wiser The "Conditional compilation" section of the Reference has a list of configurations that must be defined (as of Rust 1.14) : target_arch with values like: x86 x86_64 mips powerpc powerpc64 arm aarch64 target_os with values

Why are Rust executables so huge?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 17:11:26
Just having found Rust and having read the first two chapters of the documentation, I find the approach and the way they defined the language particularly interesting. So I decided to get my fingers wet and started out with Hello world... I did so on Windows 7 x64, btw. fn main() { println!("Hello, world!"); } Issuing cargo build and looking at the result in targets\debug I found the resulting .exe being 3MB. After some searching (documentation of cargo command line flags is hard to find...) I found --release option and created the release build. To my surprise, the .exe size has only become

How can a Rust program access metadata from its Cargo package?

徘徊边缘 提交于 2019-11-27 17:02:24
How do you access a Cargo package's metadata (e.g. version) from the Rust code in the package? In my case, I am building a command line tool that I'd like to have a standard --version flag, and I'd like the implementation to read the version of the package from Cargo.toml so I don't have to maintain it in two places. I can imagine there are other reasons someone might want to access Cargo metadata from the program as well. Vladimir Matveev Cargo passes some metadata to the compiler through environment variables, a list of which can be found in the Cargo documentation pages . The compiler