Function save_to_file

Source
pub fn save_to_file<T: Serialize>(data: &T, path: &str) -> Result<()>
Expand description

Saves any serializable data to a json file.

This function serializes the provided data structure into a json format and writes it to the specified file path. It works with any type that implements serde::Serialize.

§Arguments

  • data - A reference to the data to serialize and save.
  • path - A string slice specifying the file path.

§Returns

A Result indicating success (Ok(())) or failure (Err) with an I/O error.

§Example

use serde::Serialize;
#[derive(Serialize)]
struct Example {
    id: u32,
    name: String,
}

let data = Example { id: 1, name: "Test".to_string() };
save_to_file(&data, "output/example.json").expect("Failed to save file");