fn save_records(file_name: PathBuf, records: Records) -> std::io::Result<()> {
    // We use OpenOptions to configure how the file should be opened.
    // This is needed so we can get write access to the file. Additionally,
    // we "truncate" the file, which deletes all the contents. This is done
    // because we rewrite the entire contents whenever we save. It is possible
    // to write to a specific section of the file, but rewriting the entire
    // file is the simplest method.
    let mut file = OpenOptions::new()
        .write(true)
        .truncate(true)
        .open(file_name)?;

    // First we write the field names.
    file.write(b"id,name,email\n")?;

    // Then we iterate through each record and write it to the file.
    // "Into_iter" creates an iterator that takes ownership of the data
    // during iteration. We do this so we don't have to make additional
    // copies of the data before saving it to disk (we can just work with
    // it directly).
    for record in records.into_vec().into_iter() {
        // When we do not have an email, we just use an empty string ("").
        let email = match record.email {
            Some(email) => email,
            None => "".to_string(),
        };
        // This creates a new string that is properly formatted to CSV.
        let line = format!("{},{},{}\n", record.id, record.name, email);
        // We then write the string to the file. "write" works with bytes,
        // so we just access the bytes of the string with "as_bytes".
        file.write(line.as_bytes())?;
    }
    // "Flushing" the data ensures that everything is written to disk before
    // continuing. Without this line, it is possible for the program to
    // terminate before the system is done writing to the file, and this
    // can result in corrupted data.
    file.flush()?;
    Ok(())
}
/// Saves the records to disk.
Saves the records to disk.
Saves the records to disk .
fn save_records(file_name: PathBuf, records: Records) -> std::io::Result<()> {
    // We use OpenOptions to configure how the file should be opened.
    // This is needed so we can get write access to the file. Additionally,
    // we "truncate" the file, which deletes all the contents. This is done
    // because we rewrite the entire contents whenever we save. It is possible
    // to write to a specific section of the file, but rewriting the entire
    // file is the simplest method.
    let mut file = OpenOptions::new()
        .write(true)
        .truncate(true)
        .open(file_name)?;
    // First we write the field names.
    file.write(b"id,name,email\n")?;
    // Then we iterate through each record and write it to the file.
    // "Into_iter" creates an iterator that takes ownership of the data
    // during iteration. We do this so we don't have to make additional
    // copies of the data before saving it to disk (we can just work with
    // it directly).
    for record in records.into_vec().into_iter() {
        // When we do not have an email, we just use an empty string ("").
        let email = match record.email {
            Some(email) => email,
            None => "".to_string(),
        };
        // This creates a new string that is properly formatted to CSV.
        let line = format!("{},{},{}\n", record.id, record.name, email);
        // We then write the string to the file. "write" works with bytes,
        // so we just access the bytes of the string with "as_bytes".
        file.write(line.as_bytes())?;
    }
    // "Flushing" the data ensures that everything is written to disk before
    // continuing. Without this line, it is possible for the program to
    // terminate before the system is done writing to the file, and this
    // can result in corrupted data.
    file.flush()?;
    Ok(())
}
fn save_records ( file_name : PathBuf , records : Records ) -> std :: io :: Result < ( ) > { // We use OpenOptions to configure how the file should be opened. // This is needed so we can get write access to the file. Additionally, // we "truncate" the file, which deletes all the contents. This is done // because we rewrite the entire contents whenever we save. It is possible // to write to a specific section of the file, but rewriting the entire // file is the simplest method. let mut file = OpenOptions :: new ( ) . write ( true ) . truncate ( true ) . open ( file_name ) ? ; // First we write the field names. file . write ( b" \n " ) ? ; // Then we iterate through each record and write it to the file. // "Into_iter" creates an iterator that takes ownership of the data // during iteration. We do this so we don't have to make additional // copies of the data before saving it to disk (we can just work with // it directly). for record in records . into_vec ( ) . into_iter ( ) { // When we do not have an email, we just use an empty string (""). let email = match record . email { Some ( email ) => email , None => " " . to_string ( ) , } ; // This creates a new string that is properly formatted to CSV. let line = format ! ( " \n " record id record name email ) ; // We then write the string to the file. "write" works with bytes, // so we just access the bytes of the string with "as_bytes". file . write ( line . as_bytes ( ) ) ? ; } // "Flushing" the data ensures that everything is written to disk before // continuing. Without this line, it is possible for the program to // terminate before the system is done writing to the file, and this // can result in corrupted data. file . flush ( ) ? ; Ok ( ( ) ) }
Saves the records to disk.
Saves the records to disk .
file_name
PathBuf
records
Records