Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

cargo-valgrind

> 包管理
Open source

A cargo subcommand, that runs valgrind and displays its output in a helpful manner. Tailored to Rust.

178 stars0 likes2 views
WebsiteGitHub

About

A cargo subcommand, that runs valgrind and displays its output in a helpful manner. Tailored to Rust.

cargo-valgrind

A cargo subcommand, that runs valgrind and collects its output in a helpful manner.

This command extends cargo with the capability to directly run valgrind on any crate executable. The output of valgrind is then used to mark the binary as pass/fail.

This command should not be necessary for ordinary Rust programs, especially if you are only using safe Rust code. But if you do FFI-related stuff (either by simply using a FFI-binding crate or because you are developing a safe wrapper for such FFI bindings) it may be really helpful to check, whether the memory usages across the FFI borders are correct.

Usage

A typical mistake would be:

rust
use std::ffi::CString;
use std::os::raw::c_char;

extern "C" {
    fn puts(s: *const c_char);
}

fn main() {
    let string = CString::new("Test").unwrap();

    let ptr = string.into_raw();
    unsafe { puts(ptr) };

    // unsafe { CString::from_raw(ptr) };
}

The memory of the variable string will never be freed. If you run cargo valgrind run in your shell, it detects the leak:

…

Un-commenting the unsafe { CString::from_raw(ptr) }; re-takes the memory and frees it correctly. cargo valgrind run will compile the binary for you and won't detect a leak, since there is no leak anymore.

If you would like to pass flags to valgrind (for example to run an alternate subtool), you can set the VALGRINDFLAGS environment variable to a space-delimited list of valid Valgrind options.

cargo valgrind automatically applies some suppressions for wrongly reported leaks within the Rust standard library. This makes the tool more powerful than a normal valgrind invocation.

Note, that in some cases, the normal destructors are not run, which can lead to memory leaks reported (as the memory is actually not freed up). This typically happens when executing std::process::exit(), which does not run any Drop impls of alive variables, thus leaking the memory. Either explicitly drop all variables before running std::process::exit() or, preferrably, migrate to returning a Result from main, which gracefully handles the dropping/resource releasing like normal functions do.

Note: users of cargo-valgrind version 1.x should mind the changed command line. Previously there was a cargo valgrind subcommand, that replaced the cargo run or cargo test commands. Now the command line is cargo valgrind , where `` can be any normal cargo subcommand.

Installation

Requirements

You need to have valgrind installed and in the PATH (you can test this by running valgrind --help in your shell).

You'll also need to have cargo installed and in the PATH, but since this is a cargo subcommand, you will almost certainly have it already installed.

Install the binary

Use pre-built binaries

Head over to the latest release and download the artifact for your platform. The binary has to be extracted into the .cargo-directory, typically under $HOME/.cargo. Note, that it is not possible to directly run the program itself, as it must be invoked via cargo valgrind, so it must be located in a directory, cargo searches its subcommands in.

From Source

Run the following command to install from crates.io:

bash
$ cargo install cargo-valgrind

This will install the latest official released version.

If you want to use the latest changes, that were not yet published to crates.io, you can install the binary from the git-repository like this:

bash
$ cargo install --git https://github.com/jfrimmel/cargo-valgrind cargo-valgrind

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in cargo-valgrind by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustcargocargo-plugincargo-subcommandcargo-valgrind

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category包管理
PricingOpen source

> Related tools

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image