Rust

On this page, we get you up and running with Sentry's Rust SDK.

If you don't already have an account and Sentry project established, head over to sentry.io, then return to this page.

Using a framework? Check out the other SDKs we support in the left-hand dropdown.

Related Guides

To add Sentry to your Rust project, just add a new dependency to your Cargo.toml:

Cargo.toml
Copied
[dependencies]
sentry = "0.32.3"

The most convenient way to use this library is the sentry::init function, which starts a Sentry client with a default set of integrations, and binds it to the current Hub.

The sentry::init function returns a guard that when dropped, will flush Events that weren't yet sent to Sentry. It has a two-second deadline, so application shutdown may be slightly delayed as a result. Be sure to keep the guard or you won't be able to send events.

main.rs
Copied
let _guard = sentry::init(("https://examplePublicKey@o0.ingest.sentry.io/0", sentry::ClientOptions {
    release: sentry::release_name!(),
    ..Default::default()
}));

Important: Remember your DSN. The DSN (Data Source Name) tells the SDK where to send events. If you forget it, you can find it by going to: Settings -> Projects -> Client Keys (DSN) in sentry.io.

In a multithreaded application, spawned threads should inherit the Hub from the main thread. In order for that to happen, the Sentry client must be initialized before starting an async runtime or spawning threads. This means you'll have to avoid using macros such as #[tokio::main] or #[actix_web::main], because they start the runtime first. So rather than doing this:

main.rs
Copied
// WRONG
#[tokio::main]
async fn main() {
let _guard = sentry::init(("https://examplePublicKey@o0.ingest.sentry.io/0", sentry::ClientOptions {
    release: sentry::release_name!(),
    ..Default::default()
}));

// implementation of main
}

Do this instead:

main.rs
Copied
// RIGHT
fn main() {
let _guard = sentry::init(("https://examplePublicKey@o0.ingest.sentry.io/0", sentry::ClientOptions {
    release: sentry::release_name!(),
    ..Default::default()
}));

tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            // implementation of main
        });
}

The quickest way to verify Sentry in your Rust application is to cause a panic:

main.rs
Copied
fn main() {
    let _guard = sentry::init(("https://examplePublicKey@o0.ingest.sentry.io/0", sentry::ClientOptions {
        release: sentry::release_name!(),
        ..Default::default()
    }));

    // Sentry will capture this
    panic!("Everything is on fire!");
}

Integrations extend the functionality of the SDK for some common frameworks and libraries.

A list of integrations and their feature flags can be found in the integration API docs.

If you keep debug information files in the produced binary, rather than strip them down or split them, do not add the debug_images integration and the corresponding debug-images feature flag, as it will collide with the built-in debug file processing.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").