2 ms·
> it would be nice if there was something like Python's with statement to correctly close a file. In C++ or Rust, non-memory resource management is arguably ev
by paulddraper 7y ago
> it would be nice if there was something like Python's with statement to correctly close a file.
In C++ or Rust, non-memory resource management is arguably even easier than Python.
RAII: Resource Acquisition Is Initialization.
(Apologies for the C++; can do the same thing in Rust.)
ManagedFile myfile("path.txt");
myfile.read();
`myfile` gets released whenever the scope finishes.
That said, the C++ and Rust stdlibs don't look like that because opening and closing files is not exception-free and unlike Python, C++ and Rust don't have or don't prefer non-explicit error handling.