All posts

Fix NotFoundError in Rust

Resolve file not found and module resolution errors in Rust projects, covering mod declarations, Cargo dependencies, and path handling.

Rust Not Found Errors

Rust's file not found for module and unresolved import errors are compile-time issues. At runtime, std::io::ErrorKind::NotFound appears when files or resources are missing.

Module Not Found

// Error: file not found for module `utils`
// Rust expects either:
// src/utils.rs
// OR
// src/utils/mod.rs

// In main.rs or lib.rs:
mod utils;  // Looks for src/utils.rs or src/utils/mod.rs

// For nested modules:
mod models {
    mod user;  // Looks for src/models/user.rs
}

Unresolved Import

// Error: unresolved import `serde`
// Fix: add to Cargo.toml
// [dependencies]
// serde = { version = "1.0", features = ["derive"] }

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct User {
    name: String,
}

Runtime File Not Found

use std::path::PathBuf;

// BAD — relative path depends on working directory
let config = std::fs::read_to_string("config.toml")?;

// GOOD — relative to the executable
let exe_dir = std::env::current_exe()?
    .parent()
    .unwrap()
    .to_path_buf();
let config_path = exe_dir.join("config.toml");
let config = std::fs::read_to_string(&config_path)
    .map_err(|e| format!("Failed to read {}: {}", config_path.display(), e))?;

Include at Compile Time

For small files, embed them in the binary:

const CONFIG: &str = include_str!("../config.toml");
const LOGO: &[u8] = include_bytes!("../assets/logo.png");

Bugsly captures Rust panics and error results with full backtraces, including io::Error with the file path that caused the failure.

Try Bugsly Free

Track up to 100 issues per month on the free plan, with unlimited events and no credit card required.

Get Started Free