Skip to main content

Rust

Connect directly to Foxglove or play back local data recorded with the Rust SDK.

Install the SDK

Install foxglove from crates.io: https://crates.io/crates/foxglove

cargo add foxglove

If you want to try Foxglove with a new project, you can use these steps:

cargo new example --bin
cd example
cargo add foxglove

Live data

Log messages from Rust

Add the following to main.rs:

use std::{thread, time};

#[derive(foxglove::Encode)]
struct Message {
elapsed: f64,
}

fn main() {
foxglove::WebSocketServer::new()
.start_blocking()
.expect("Server failed to start");

let start = time::SystemTime::now();
loop {
foxglove::log!(
"/hello",
Message {
elapsed: start
.elapsed()
.expect("clock failed to elapse")
.as_secs_f64()
}
);
thread::sleep(time::Duration::from_millis(30));
}
}

Run the example

From the project directory:

cargo run

Connect

In Foxglove, select "Open connection" from the dashboard or left-hand menu.

Select open connection

Select "Foxglove WebSocket" in the "Open a new connection" dialog, then click "Open" to accept the default connection string:

Foxglove WebSocket dialog

Local data

Recording data to a file

The SDK can also log data to disk. Let's augment the example above to also produce an MCAP file which can later be opened in Foxglove:

use std::{thread, time};

#[derive(foxglove::Encode)]
struct Message {
elapsed: f64,
}

fn main() {
foxglove::WebSocketServer::new()
.start_blocking()
.expect("Server failed to start");

// Keep a reference to the writer. It'll automatically flush and close when it's dropped,
// or we could call `.close()` to close it manually.
let _ = foxglove::McapWriter::new()
.create_new_buffered_file("example.mcap")
.expect("Failed to create writer");

let start = time::SystemTime::now();
loop {
foxglove::log!(
"/hello",
Message {
elapsed: start
.elapsed()
.expect("clock failed to elapse")
.as_secs_f64()
}
);
thread::sleep(time::Duration::from_millis(30));
}
}

Viewing data from a file

To load local files for visualization, you can:

  • Click "Open local file(s)…" in the dashboard or left-hand menu
  • Open or drag-and-drop the files from your OS file manager (desktop only)

Local file dialog

note

When opening multiple files, Foxglove will merge the data into a single playback timeline.

You can only merge multiple files of the same format.

Imported data

After importing data to Foxglove, select individual resources to visualize on the Recordings or Events pages:

Visualize on Recordings page

Select a custom time range of data (can span multiple recordings or events) to visualize on the Timeline page: