Rust ZeroMQ bindings.
rust-zmq is available from crates.io. Users
should add this to their Cargo.toml
file:
[dependencies]
zmq = "0.9"
As rust-zmq is a wrapper around libzmq
, you need a build of libzmq
version 4.1 or newer, before attempting to build the zmq
crate. There are several options available:
This is probably the preferred method when you are running a recent
Unix-like OS that has support for pkg-config
. For example, on recent
Debian-based distributions, you can use the following command to get
the prerequiste headers and library installed:
apt install libzmq3-dev
If your OS of choice does not provide packages of a new-enough libzmq,
you can install it from source; see
https://github.com/zeromq/libzmq/releases, although in this case,
you may prefer a vendored
build, which automates that, see below.
The build normally uses pkg-config
to find out about libzmq's
location. If that is not available, the environment variable
LIBZMQ_PREFIX
(or alternatively, LIBZMQ_LIB_DIR
and
LIBZMQ_INCLUDE_DIR
) can be defined to avoid the invocation of
pkg-config
.
When building on Windows, using the MSCV toolchain, consider the
following when trying to link dynamically against libzmq
:
- When building
libzmq
from sources, the library must be renamed tozmq.lib
from the auto namedlibzmq-v***-mt-gd-*_*_*.lib
,libzmq.lib
,libzmq-mt-*_*_*.lib
, etc. - The folder containing the
*.dll
(dynamic link library) referred to byzmq.lib
must be accessible via the path for the session that invokes the Rust compiler. - The name of the
*.dll
in question depends on the build system used forlibzmq
and can usually be seen when openingzmq.lib
in a text editor.
Starting with the upcoming release 0.9.1
(or when building from
current master
), you can enable the vendored
feature flag to have
libzmq
be built for you and statically linked into your binary
crate. In your Cargo.toml
, you can give users the option to do so
using a dedicated feature flag:
[features]
vendored-zmq = ['zmq/vendored']
When you have a cross-compiled version of libzmq
installed, you
should be able to cross-compile rust-zmq, assuming a platform
supporting pkg-config
. For example, assuming you have libzmq
compiled for the i686-pc-windows-gnu
target installed in
~/.local-w32
, the following should work:
PKG_CONFIG_PATH=$HOME/.local-w32/lib/pkgconfig \
PKG_CONFIG_ALLOW_CROSS=1 \
cargo build --target=i686-pc-windows-gnu --verbose
Cross compilation without pkg-config
should work as well, but you
need set LIBZMQ_PREFIX
as described above.
rust-zmq
is a pretty straight forward port of the C API into Rust:
fn main() {
let ctx = zmq::Context::new();
let mut socket = ctx.socket(zmq::REQ).unwrap();
socket.connect("tcp://127.0.0.1:1234").unwrap();
socket.send("hello world!", 0).unwrap();
}
You can find more usage examples in https://github.com/erickt/rust-zmq/tree/master/examples.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed under the terms of both the Apache License, Version 2.0 and the MIT license without any additional terms or conditions.
See the contribution guidelines for what to watch out for when submitting a pull request.