Writing a simple vAccel Rust application🔗
For a simple example of using the vaccel Rust crate, you can replicate in Rust an image classification application similar to the image classification example from Running the examples.
Initialize a new rust-vaccel-classify project and module:
You should get the following file/directory structure:
Edit the Cargo.toml file with the following content:
Cargo.toml
[package]
name = "rust-vaccel-classify"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
[features]
default = ["profiling"]
profiling = []
[dependencies]
vaccel = { git = "https://github.com/nubificus/vaccel-rust" }
protobuf = "3.1"
env_logger = "0.11"
log = "0.4"
libc = "0.2"
thiserror = "1.0"
[build-dependencies]
libc = "0.2"
bindgen = "0.69"
pkg-config = "0.3"
[dev-dependencies]
env_logger = "0.11"
log = "0.4"
Edit the necessary logic into src/main.rs:
src/main.rs
// SPDX-License-Identifier: Apache-2.0
use env_logger::Env;
use log::{error, info};
use std::env;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use vaccel::session::Session;
fn main() {
env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init();
info!("Starting vAccel classification example");
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
error!("Usage: {} <image_path>", args[0]);
return;
}
let image_path = &args[1];
if !Path::new(image_path).exists() {
error!("Image file not found: {}", image_path);
return;
}
info!("Creating new vAccel session");
let mut sess = match Session::new() {
Ok(sess) => sess,
Err(e) => {
error!("Error creating session: {}", e);
return;
}
};
info!("Initialized session {}", sess.id().unwrap());
info!("Reading image file: {}", image_path);
let mut file = match File::open(image_path) {
Ok(file) => file,
Err(e) => {
error!("Failed to open image file: {}", e);
return;
}
};
let mut image_data = Vec::new();
if let Err(e) = file.read_to_end(&mut image_data) {
error!("Failed to read image data: {}", e);
return;
}
info!("Read image data: {} bytes", image_data.len());
info!("Performing image classification");
let ret = sess.image_classification(&image_data);
match ret {
Ok((first_vec, second_vec)) => {
let first_str = String::from_utf8_lossy(&first_vec);
let second_str = String::from_utf8_lossy(&second_vec);
info!("Classification completed successfully");
println!("Classification tags: {}", first_str);
println!("Annotated image: {}", second_str);
}
Err(e) => {
error!("Classification failed: {:?}", e);
}
}
}
The session is released automatically when sess is dropped at the end of main().
Build it:
$ cargo build
Updating crates.io index
Updating git repository `https://github.com/nubificus/vaccel-rust`
Compiling proc-macro2 v1.0.106
Compiling unicode-ident v1.0.24
Compiling autocfg v1.4.0
Compiling cfg-if v1.0.0
[snipped]
Compiling protobuf v3.7.2
Compiling protobuf-support v3.7.2
Compiling vaccel v0.0.0 (https://github.com/nubificus/vaccel-rust)
Compiling dashmap v6.1.0
Compiling env_logger v0.11.10
Compiling rust-vaccel-classify v0.1.0 (/tmp/rust-vaccel-classify)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 25.90s
And you are ready to run your application.
Configure vAccel:
and run with:
$ cargo run /usr/local/share/vaccel/images/example.jpg
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
Running `target/debug/rust-vaccel-classify /usr/local/share/vaccel/images/example.jpg`
[2026-04-29T14:57:01Z INFO rust_vaccel_classify] Starting vAccel classification example
[2026-04-29T14:57:01Z INFO rust_vaccel_classify] Creating new vAccel session
2026.04.29-14:57:01.16 - <debug> Initializing vAccel
2026.04.29-14:57:01.16 - <info> vAccel 0.7.1-93-ebc23b1f
2026.04.29-14:57:01.16 - <debug> Config:
2026.04.29-14:57:01.16 - <debug> plugins = libvaccel-noop.so
2026.04.29-14:57:01.16 - <debug> log_level = debug
2026.04.29-14:57:01.16 - <debug> log_file = (null)
2026.04.29-14:57:01.16 - <debug> profiling_enabled = false
2026.04.29-14:57:01.16 - <debug> version_ignore = false
2026.04.29-14:57:01.16 - <debug> Created top-level rundir: /run/user/0/vaccel/kZ5YUT
2026.04.29-14:57:01.16 - <info> Registered plugin noop 0.7.1-93-ebc23b1f
2026.04.29-14:57:01.16 - <debug> Registered op noop from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op exec from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op exec_with_resource from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op image_classify from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op image_detect from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op image_segment from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op image_pose from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op image_depth from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op tf_model_load from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op tf_model_unload from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op tf_model_run from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op tflite_model_load from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op tflite_model_unload from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op tflite_model_run from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op torch_model_load from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op torch_model_run from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op torch_sgemm from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op blas_sgemm from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op fpga_arraycopy from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op fpga_vectoradd from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op fpga_parallel from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op fpga_mmult from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op minmax from plugin noop
2026.04.29-14:57:01.16 - <debug> Registered op opencv from plugin noop
2026.04.29-14:57:01.16 - <debug> Loaded plugin noop from libvaccel-noop.so
2026.04.29-14:57:01.16 - <debug> New rundir for session 1: /run/user/0/vaccel/kZ5YUT/session.1
2026.04.29-14:57:01.16 - <debug> Initialized session 1 with plugin noop
[2026-04-29T14:57:01Z INFO rust_vaccel_classify] Initialized session 1
[2026-04-29T14:57:01Z INFO rust_vaccel_classify] Reading image file: /usr/local/share/vaccel/images/example.jpg
[2026-04-29T14:57:01Z INFO rust_vaccel_classify] Read image data: 79281 bytes
[2026-04-29T14:57:01Z INFO rust_vaccel_classify] Performing image classification
2026.04.29-14:57:01.16 - <debug> session:1 Looking for func implementing op image_classify
2026.04.29-14:57:01.16 - <debug> Returning func for op image_classify from plugin noop
2026.04.29-14:57:01.16 - <debug> [noop] Calling Image classification for session 1
2026.04.29-14:57:01.16 - <debug> [noop] Dumping arguments for Image classification:
2026.04.29-14:57:01.16 - <debug> [noop] model: (null)
2026.04.29-14:57:01.16 - <debug> [noop] len_img: 79281
2026.04.29-14:57:01.16 - <debug> [noop] len_out_text: 1024
2026.04.29-14:57:01.16 - <debug> [noop] len_out_imgname: 1024
2026.04.29-14:57:01.16 - <debug> [noop] will return a dummy result
2026.04.29-14:57:01.16 - <debug> [noop] will return a dummy result
[2026-04-29T14:57:01Z INFO rust_vaccel_classify] Classification completed successfully
Classification tags: This is a dummy classification tag!
Annotated image: This is a dummy imgname!
2026.04.29-14:57:01.16 - <debug> Released session 1
2026.04.29-14:57:01.16 - <debug> Cleaning up vAccel
2026.04.29-14:57:01.16 - <debug> Cleaning up sessions
2026.04.29-14:57:01.16 - <debug> Cleaning up resources
2026.04.29-14:57:01.16 - <debug> Cleaning up plugins
2026.04.29-14:57:01.16 - <debug> Unregistered plugin noop
The vAccel output of classify and rust-vaccel-classify should be almost identical.