The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Append To A File In Rust

TODO: Pull subtitle into page object code_start_default_section code_end_default_section

Notes

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Append To A File In Rust

This is how I append to a file in Rust

-- code/
-- rust

use std::fs::OpenOptions;
use std::io::Write;

fn main() {
    append_file("output.txt", "alfa bravo charlie");
}

fn append_file(path: &str, text: &str) {
    println!("Appending file");
    if let Ok(mut file) = OpenOptions::new()
        .create(true).append(true).open(path) {
        if let Err(e) = writeln!(file, "{}", text) {
            eprintln!("Couldn't append: {}", e);
        }
    }
}

-- /code


-- notes

- The `create(true)`` makes the file if it 
doesn't already exist. Without that, the
process will throw an error if the file
isn't already there. 

- Could also update it to return 
a result for things working or not. I
haven't needed that yet. I expect it'll
come in hand at some point.


-- ref
-- url: https://doc.rust-lang.org/std/fs/struct.File.html


-- ref
-- url: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html

-- ref
-- url: https://doc.rust-lang.org/stable/std/io/trait.Write.html



-- categories
-- Rust 

-- metadata
-- date: 2023-08-05 19:54:58
-- id: 2tassbf1
-- site: aws
-- type: post
-- status: published