Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs::append #556

Open
lolbinarycat opened this issue Mar 7, 2025 · 3 comments
Open

fs::append #556

lolbinarycat opened this issue Mar 7, 2025 · 3 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@lolbinarycat
Copy link

Proposal

Problem statement

There is no concise way to write a simple "open and append to file" operation.

Motivating examples or use cases

  1. Placeholder logging implementation
  2. A command that runs multiple times, and each run appends a short string to a specific file, so the results can be collected and interpreted later.
  3. Providing an equivalent to >> when line-for-line translating a shell script into rust.

Solution sketch

// in std::fs

pub fn append<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()>

This would be a parallel to the existing fs::write and fs::read.

It would create the file if it does not exist.

Alternatives

Create File::open_append as a parallel to File::create and File::create_new.

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@lolbinarycat lolbinarycat added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Mar 7, 2025
@scottmcm
Copy link
Member

scottmcm commented Mar 7, 2025

Beware that appending a file in a loop like that is a Shlemiel the painter algorithm on Windows (and perhaps others) as the virus scanner will re-scan the whole file every time.

This should absolutely not be used as a "Placeholder logging implementation"; even in a placeholder it should keep the file open.

@lolbinarycat
Copy link
Author

It seems like it would still be fine on linux, no? this pattern is quite common in shell scripts.

@the8472
Copy link
Member

the8472 commented Mar 7, 2025

Alternatives

Create File::open_append as

#512 has already been approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants