pub struct File {/* private fields */ }
Expand description
An object providing access to an open file on the filesystem.
An instance of aFile
can be read and/or written depending on what optionsit was opened with. Files also implementSeek
to alter the logical cursorthat the file contains internally.
Files are automatically closed when they go out of scope. Errors detectedon closing are ignored by the implementation ofDrop
. Use the methodsync_all
if these errors must be manually handled.
File
does not buffer reads and writes. For efficiency, consider wrapping thefile in aBufReader
orBufWriter
when performing many smallread
orwrite
calls, unless unbuffered reads and writes are required.
§Examples
Creates a new file and write bytes to it (you can also usewrite
):
usestd::fs::File;usestd::io::prelude::*;fnmain() -> std::io::Result<()> {letmutfile = File::create("foo.txt")?; file.write_all(b"Hello, world!")?;Ok(())}
Reads the contents of a file into aString
(you can also useread
):
usestd::fs::File;usestd::io::prelude::*;fnmain() -> std::io::Result<()> {letmutfile = File::open("foo.txt")?;letmutcontents = String::new(); file.read_to_string(&mutcontents)?;assert_eq!(contents,"Hello, world!");Ok(())}
Using a bufferedRead
er:
usestd::fs::File;usestd::io::BufReader;usestd::io::prelude::*;fnmain() -> std::io::Result<()> {letfile = File::open("foo.txt")?;letmutbuf_reader = BufReader::new(file);letmutcontents = String::new(); buf_reader.read_to_string(&mutcontents)?;assert_eq!(contents,"Hello, world!");Ok(())}
Note that, although read and write methods require a&mut File
, becauseof the interfaces forRead
andWrite
, the holder of a&File
canstill modify the file, either through methods that take&File
or byretrieving the underlying OS object and modifying the file that way.Additionally, many operating systems allow concurrent modification of filesby different processes. Avoid assuming that holding a&File
means that thefile will not change.
§Platform-specific behavior
On Windows, the implementation ofRead
andWrite
traits forFile
perform synchronous I/O operations. Therefore the underlying file must nothave been opened for asynchronous I/O (e.g. by usingFILE_FLAG_OVERLAPPED
).
Implementations§
Source§implFile
implFile
1.0.0 ·Sourcepub fnopen<P:AsRef<Path>>(path: P) ->Result<File>
pub fnopen<P:AsRef<Path>>(path: P) ->Result<File>
Attempts to open a file in read-only mode.
See theOpenOptions::open
method for more details.
If you only need to read the entire file contents,considerstd::fs::read()
orstd::fs::read_to_string()
instead.
§Errors
This function will return an error ifpath
does not already exist.Other errors may also be returned according toOpenOptions::open
.
§Examples
Sourcepub fnopen_buffered<P:AsRef<Path>>(path: P) ->Result<BufReader<File>>
🔬This is a nightly-only experimental API. (file_buffered
#130804)
pub fnopen_buffered<P:AsRef<Path>>(path: P) ->Result<BufReader<File>>
file_buffered
#130804)Attempts to open a file in read-only mode with buffering.
See theOpenOptions::open
method, theBufReader
type,and theBufRead
trait for more details.
If you only need to read the entire file contents,considerstd::fs::read()
orstd::fs::read_to_string()
instead.
§Errors
This function will return an error ifpath
does not already exist,or if memory allocation fails for the new buffer.Other errors may also be returned according toOpenOptions::open
.
§Examples
1.0.0 ·Sourcepub fncreate<P:AsRef<Path>>(path: P) ->Result<File>
pub fncreate<P:AsRef<Path>>(path: P) ->Result<File>
Opens a file in write-only mode.
This function will create a file if it does not exist,and will truncate it if it does.
Depending on the platform, this function may fail if thefull directory path does not exist.See theOpenOptions::open
function for more details.
See alsostd::fs::write()
for a simple function tocreate a file with some given data.
§Examples
Sourcepub fncreate_buffered<P:AsRef<Path>>(path: P) ->Result<BufWriter<File>>
🔬This is a nightly-only experimental API. (file_buffered
#130804)
pub fncreate_buffered<P:AsRef<Path>>(path: P) ->Result<BufWriter<File>>
file_buffered
#130804)Opens a file in write-only mode with buffering.
This function will create a file if it does not exist,and will truncate it if it does.
Depending on the platform, this function may fail if thefull directory path does not exist.
See theOpenOptions::open
method and theBufWriter
type for more details.
See alsostd::fs::write()
for a simple function tocreate a file with some given data.
§Examples
1.77.0 ·Sourcepub fncreate_new<P:AsRef<Path>>(path: P) ->Result<File>
pub fncreate_new<P:AsRef<Path>>(path: P) ->Result<File>
Creates a new file in read-write mode; error if the file exists.
This function will create a file if it does not exist, or return an error if it does. Thisway, if the call succeeds, the file returned is guaranteed to be new.If a file exists at the target location, creating a new file will fail withAlreadyExists
or another error based on the situation. SeeOpenOptions::open
for anon-exhaustive list of likely errors.
This option is useful because it is atomic. Otherwise between checking whether a fileexists and creating a new one, the file may have been created by another process (a TOCTOUrace condition / attack).
This can also be written usingFile::options().read(true).write(true).create_new(true).open(...)
.
§Examples
1.58.0 ·Sourcepub fnoptions() ->OpenOptions
pub fnoptions() ->OpenOptions
Returns a new OpenOptions object.
This function returns a new OpenOptions object that you can use toopen or create a file with specific options ifopen()
orcreate()
are not appropriate.
It is equivalent toOpenOptions::new()
, but allows you to write morereadable code. Instead ofOpenOptions::new().append(true).open("example.log")
,you can writeFile::options().append(true).open("example.log")
. Thisalso avoids the need to importOpenOptions
.
See theOpenOptions::new
function for more details.
§Examples
1.0.0 ·Sourcepub fnsync_all(&self) ->Result<()>
pub fnsync_all(&self) ->Result<()>
Attempts to sync all OS-internal file content and metadata to disk.
This function will attempt to ensure that all in-memory data reaches thefilesystem before returning.
This can be used to handle errors that would otherwise only be caughtwhen theFile
is closed, as dropping aFile
will ignore all errors.Note, however, thatsync_all
is generally more expensive than closinga file by dropping it, because the latter is not required to block untilthe data has been written to the filesystem.
If synchronizing the metadata is not required, usesync_data
instead.
§Examples
1.0.0 ·Sourcepub fnsync_data(&self) ->Result<()>
pub fnsync_data(&self) ->Result<()>
This function is similar tosync_all
, except that it might notsynchronize file metadata to the filesystem.
This is intended for use cases that must synchronize content, but don’tneed the metadata on disk. The goal of this method is to reduce diskoperations.
Note that some platforms may simply implement this in terms ofsync_all
.
§Examples
Sourcepub fnlock(&self) ->Result<()>
🔬This is a nightly-only experimental API. (file_lock
#130994)
pub fnlock(&self) ->Result<()>
file_lock
#130994)Acquire an exclusive lock on the file. Blocks until the lock can be acquired.
This acquires an exclusive lock; no other file handle to this file may acquire another lock.
This lock may be advisory or mandatory. This lock is meant to interact withlock
,try_lock
,lock_shared
,try_lock_shared
, andunlock
. Its interactions withother methods, such asread
andwrite
are platform specific, and it may or may notcause non-lockholders to block.
If this file handle/descriptor, or a clone of it, already holds an lock the exact behavioris unspecified and platform dependent, including the possibility that it will deadlock.However, if this method returns, then an exclusive lock is held.
If the file not open for writing, it is unspecified whether this function returns an error.
The lock will be released when this file (along with any other file descriptors/handlesduplicated or inherited from it) is closed, or if theunlock
method is called.
§Platform-specific behavior
This function currently corresponds to theflock
function on Unix with theLOCK_EX
flag,and theLockFileEx
function on Windows with theLOCKFILE_EXCLUSIVE_LOCK
flag. Note that,thismay change in the future.
On Windows, locking a file will fail if the file is opened only for append. To lock a file,open it with one of.read(true)
,.read(true).append(true)
, or.write(true)
.
§Examples
Sourcepub fnlock_shared(&self) ->Result<()>
🔬This is a nightly-only experimental API. (file_lock
#130994)
pub fnlock_shared(&self) ->Result<()>
file_lock
#130994)Acquire a shared (non-exclusive) lock on the file. Blocks until the lock can be acquired.
This acquires a shared lock; more than one file handle may hold a shared lock, but none mayhold an exclusive lock at the same time.
This lock may be advisory or mandatory. This lock is meant to interact withlock
,try_lock
,lock_shared
,try_lock_shared
, andunlock
. Its interactions withother methods, such asread
andwrite
are platform specific, and it may or may notcause non-lockholders to block.
If this file handle/descriptor, or a clone of it, already holds an lock, the exact behavioris unspecified and platform dependent, including the possibility that it will deadlock.However, if this method returns, then a shared lock is held.
The lock will be released when this file (along with any other file descriptors/handlesduplicated or inherited from it) is closed, or if theunlock
method is called.
§Platform-specific behavior
This function currently corresponds to theflock
function on Unix with theLOCK_SH
flag,and theLockFileEx
function on Windows. Note that, thismay change in the future.
On Windows, locking a file will fail if the file is opened only for append. To lock a file,open it with one of.read(true)
,.read(true).append(true)
, or.write(true)
.
§Examples
Sourcepub fntry_lock(&self) ->Result<(),TryLockError>
🔬This is a nightly-only experimental API. (file_lock
#130994)
pub fntry_lock(&self) ->Result<(),TryLockError>
file_lock
#130994)Try to acquire an exclusive lock on the file.
ReturnsErr(TryLockError::WouldBlock)
if a different lock is already held on this file(via another handle/descriptor).
This acquires an exclusive lock; no other file handle to this file may acquire another lock.
This lock may be advisory or mandatory. This lock is meant to interact withlock
,try_lock
,lock_shared
,try_lock_shared
, andunlock
. Its interactions withother methods, such asread
andwrite
are platform specific, and it may or may notcause non-lockholders to block.
If this file handle/descriptor, or a clone of it, already holds an lock, the exact behavioris unspecified and platform dependent, including the possibility that it will deadlock.However, if this method returnsOk(true)
, then it has acquired an exclusive lock.
If the file not open for writing, it is unspecified whether this function returns an error.
The lock will be released when this file (along with any other file descriptors/handlesduplicated or inherited from it) is closed, or if theunlock
method is called.
§Platform-specific behavior
This function currently corresponds to theflock
function on Unix with theLOCK_EX
andLOCK_NB
flags, and theLockFileEx
function on Windows with theLOCKFILE_EXCLUSIVE_LOCK
andLOCKFILE_FAIL_IMMEDIATELY
flags. Note that, thismay change in the future.
On Windows, locking a file will fail if the file is opened only for append. To lock a file,open it with one of.read(true)
,.read(true).append(true)
, or.write(true)
.
§Examples
Sourcepub fntry_lock_shared(&self) ->Result<(),TryLockError>
🔬This is a nightly-only experimental API. (file_lock
#130994)
pub fntry_lock_shared(&self) ->Result<(),TryLockError>
file_lock
#130994)Try to acquire a shared (non-exclusive) lock on the file.
ReturnsErr(TryLockError::WouldBlock)
if a different lock is already held on this file(via another handle/descriptor).
This acquires a shared lock; more than one file handle may hold a shared lock, but none mayhold an exclusive lock at the same time.
This lock may be advisory or mandatory. This lock is meant to interact withlock
,try_lock
,lock_shared
,try_lock_shared
, andunlock
. Its interactions withother methods, such asread
andwrite
are platform specific, and it may or may notcause non-lockholders to block.
If this file handle, or a clone of it, already holds an lock, the exact behavior isunspecified and platform dependent, including the possibility that it will deadlock.However, if this method returnsOk(true)
, then it has acquired a shared lock.
The lock will be released when this file (along with any other file descriptors/handlesduplicated or inherited from it) is closed, or if theunlock
method is called.
§Platform-specific behavior
This function currently corresponds to theflock
function on Unix with theLOCK_SH
andLOCK_NB
flags, and theLockFileEx
function on Windows with theLOCKFILE_FAIL_IMMEDIATELY
flag. Note that, thismay change in the future.
On Windows, locking a file will fail if the file is opened only for append. To lock a file,open it with one of.read(true)
,.read(true).append(true)
, or.write(true)
.
§Examples
Sourcepub fnunlock(&self) ->Result<()>
🔬This is a nightly-only experimental API. (file_lock
#130994)
pub fnunlock(&self) ->Result<()>
file_lock
#130994)Release all locks on the file.
All locks are released when the file (along with any other file descriptors/handlesduplicated or inherited from it) is closed. This method allows releasing locks withoutclosing the file.
If no lock is currently held via this file descriptor/handle, this method may return anerror, or may return successfully without taking any action.
§Platform-specific behavior
This function currently corresponds to theflock
function on Unix with theLOCK_UN
flag,and theUnlockFile
function on Windows. Note that, thismay change in the future.
On Windows, locking a file will fail if the file is opened only for append. To lock a file,open it with one of.read(true)
,.read(true).append(true)
, or.write(true)
.
§Examples
1.0.0 ·Sourcepub fnset_len(&self, size:u64) ->Result<()>
pub fnset_len(&self, size:u64) ->Result<()>
Truncates or extends the underlying file, updating the size ofthis file to becomesize
.
If thesize
is less than the current file’s size, then the file willbe shrunk. If it is greater than the current file’s size, then the filewill be extended tosize
and have all of the intermediate data filledin with 0s.
The file’s cursor isn’t changed. In particular, if the cursor was at theend and the file is shrunk using this operation, the cursor will now bepast the end.
§Errors
This function will return an error if the file is not opened for writing.Also,std::io::ErrorKind::InvalidInput
will be returned if the desired length would cause an overflow due tothe implementation specifics.
§Examples
usestd::fs::File;fnmain() -> std::io::Result<()> {letmutf = File::create("foo.txt")?; f.set_len(10)?;Ok(())}
Note that this method alters the content of the underlying file, eventhough it takes&self
rather than&mut self
.
1.0.0 ·Sourcepub fnmetadata(&self) ->Result<Metadata>
pub fnmetadata(&self) ->Result<Metadata>
Queries metadata about the underlying file.
§Examples
1.9.0 ·Sourcepub fntry_clone(&self) ->Result<File>
pub fntry_clone(&self) ->Result<File>
Creates a newFile
instance that shares the same underlying file handleas the existingFile
instance. Reads, writes, and seeks will affectbothFile
instances simultaneously.
§Examples
Creates two handles for a file namedfoo.txt
:
usestd::fs::File;fnmain() -> std::io::Result<()> {letmutfile = File::open("foo.txt")?;letfile_copy = file.try_clone()?;Ok(())}
Assuming there’s a file namedfoo.txt
with contentsabcdef\n
, createtwo handles, seek one of them, and read the remaining bytes from theother handle:
1.16.0 ·Sourcepub fnset_permissions(&self, perm:Permissions) ->Result<()>
pub fnset_permissions(&self, perm:Permissions) ->Result<()>
Changes the permissions on the underlying file.
§Platform-specific behavior
This function currently corresponds to thefchmod
function on Unix andtheSetFileInformationByHandle
function on Windows. Note that, thismay change in the future.
§Errors
This function will return an error if the user lacks permission changeattributes on the underlying file. It may also return an error in otheros-specific unspecified cases.
§Examples
fnmain() -> std::io::Result<()> {usestd::fs::File;letfile = File::open("foo.txt")?;letmutperms = file.metadata()?.permissions(); perms.set_readonly(true); file.set_permissions(perms)?;Ok(())}
Note that this method alters the permissions of the underlying file,even though it takes&self
rather than&mut self
.
1.75.0 ·Sourcepub fnset_times(&self, times:FileTimes) ->Result<()>
pub fnset_times(&self, times:FileTimes) ->Result<()>
Changes the timestamps of the underlying file.
§Platform-specific behavior
This function currently corresponds to thefutimens
function on Unix (falling back tofutimes
on macOS before 10.13) and theSetFileTime
function on Windows. Note that thismay change in the future.
§Errors
This function will return an error if the user lacks permission to change timestamps on theunderlying file. It may also return an error in other os-specific unspecified cases.
This function may return an error if the operating system lacks support to change one ormore of the timestamps set in theFileTimes
structure.
§Examples
1.75.0 ·Sourcepub fnset_modified(&self, time:SystemTime) ->Result<()>
pub fnset_modified(&self, time:SystemTime) ->Result<()>
Changes the modification time of the underlying file.
This is an alias forset_times(FileTimes::new().set_modified(time))
.
Trait Implementations§
1.63.0 ·Source§implAsFd forFile
implAsFd forFile
Source§fnas_fd(&self) ->BorrowedFd<'_>
fnas_fd(&self) ->BorrowedFd<'_>
1.63.0 ·Source§implAsHandle forFile
Available onWindows only.
implAsHandle forFile
Source§fnas_handle(&self) ->BorrowedHandle<'_>
fnas_handle(&self) ->BorrowedHandle<'_>
1.0.0 ·Source§implAsRawHandle forFile
Available onWindows only.
implAsRawHandle forFile
Source§fnas_raw_handle(&self) ->RawHandle
fnas_raw_handle(&self) ->RawHandle
1.15.0 ·Source§implFileExt forFile
Available onUnix only.
implFileExt forFile
Source§fnread_at(&self, buf: &mut [u8], offset:u64) ->Result<usize>
fnread_at(&self, buf: &mut [u8], offset:u64) ->Result<usize>
Source§fnread_vectored_at( &self, bufs: &mut [IoSliceMut<'_>], offset:u64,) ->Result<usize>
fnread_vectored_at( &self, bufs: &mut [IoSliceMut<'_>], offset:u64,) ->Result<usize>
unix_file_vectored_at
#89517)read_at
, except that it reads into a slice of buffers.Read moreSource§fnwrite_at(&self, buf: &[u8], offset:u64) ->Result<usize>
fnwrite_at(&self, buf: &[u8], offset:u64) ->Result<usize>
Source§fnwrite_vectored_at(&self, bufs: &[IoSlice<'_>], offset:u64) ->Result<usize>
fnwrite_vectored_at(&self, bufs: &[IoSlice<'_>], offset:u64) ->Result<usize>
unix_file_vectored_at
#89517)write_at
, except that it writes from a slice of buffers.Read moreSource§implFileExt forFile
Available onWASI only.
implFileExt forFile
Source§fnread_vectored_at( &self, bufs: &mut [IoSliceMut<'_>], offset:u64,) ->Result<usize>
fnread_vectored_at( &self, bufs: &mut [IoSliceMut<'_>], offset:u64,) ->Result<usize>
wasi_ext
#71213)Source§fnwrite_vectored_at(&self, bufs: &[IoSlice<'_>], offset:u64) ->Result<usize>
fnwrite_vectored_at(&self, bufs: &[IoSlice<'_>], offset:u64) ->Result<usize>
wasi_ext
#71213)Source§fnfdstat_set_flags(&self, flags:u16) ->Result<()>
fnfdstat_set_flags(&self, flags:u16) ->Result<()>
wasi_ext
#71213)Source§fnfdstat_set_rights(&self, rights:u64, inheriting:u64) ->Result<()>
fnfdstat_set_rights(&self, rights:u64, inheriting:u64) ->Result<()>
wasi_ext
#71213)Source§fnadvise(&self, offset:u64, len:u64, advice:u8) ->Result<()>
fnadvise(&self, offset:u64, len:u64, advice:u8) ->Result<()>
wasi_ext
#71213)Source§fnallocate(&self, offset:u64, len:u64) ->Result<()>
fnallocate(&self, offset:u64, len:u64) ->Result<()>
wasi_ext
#71213)Source§fncreate_directory<P:AsRef<Path>>(&self, dir: P) ->Result<()>
fncreate_directory<P:AsRef<Path>>(&self, dir: P) ->Result<()>
wasi_ext
#71213)Source§fnread_link<P:AsRef<Path>>(&self, path: P) ->Result<PathBuf>
fnread_link<P:AsRef<Path>>(&self, path: P) ->Result<PathBuf>
wasi_ext
#71213)Source§fnmetadata_at<P:AsRef<Path>>( &self, lookup_flags:u32, path: P,) ->Result<Metadata>
fnmetadata_at<P:AsRef<Path>>( &self, lookup_flags:u32, path: P,) ->Result<Metadata>
wasi_ext
#71213)Source§fnremove_file<P:AsRef<Path>>(&self, path: P) ->Result<()>
fnremove_file<P:AsRef<Path>>(&self, path: P) ->Result<()>
wasi_ext
#71213)Source§fnremove_directory<P:AsRef<Path>>(&self, path: P) ->Result<()>
fnremove_directory<P:AsRef<Path>>(&self, path: P) ->Result<()>
wasi_ext
#71213)Source§fnread_at(&self, buf: &mut [u8], offset:u64) ->Result<usize>
fnread_at(&self, buf: &mut [u8], offset:u64) ->Result<usize>
wasi_ext
#71213)1.33.0 ·Source§fnread_exact_at(&self, buf: &mut [u8], offset:u64) ->Result<()>
fnread_exact_at(&self, buf: &mut [u8], offset:u64) ->Result<()>
buf
from the given offset.Read more1.63.0 ·Source§implFrom<File> forOwnedHandle
Available onWindows only.
implFrom<File> forOwnedHandle
1.20.0 ·Source§implFrom<File> forStdio
implFrom<File> forStdio
1.63.0 ·Source§implFrom<OwnedHandle> forFile
Available onWindows only.
implFrom<OwnedHandle> forFile
Source§fnfrom(owned:OwnedHandle) -> Self
fnfrom(owned:OwnedHandle) -> Self
Returns aFile
that takes ownership of the given handle.
1.1.0 ·Source§implFromRawHandle forFile
Available onWindows only.
implFromRawHandle forFile
1.4.0 ·Source§implIntoRawFd forFile
implIntoRawFd forFile
Source§fninto_raw_fd(self) ->RawFd
fninto_raw_fd(self) ->RawFd
1.4.0 ·Source§implIntoRawHandle forFile
Available onWindows only.
implIntoRawHandle forFile
Source§fninto_raw_handle(self) ->RawHandle
fninto_raw_handle(self) ->RawHandle
1.70.0 ·Source§implIsTerminal forFile
implIsTerminal forFile
Source§fnis_terminal(&self) ->bool
fnis_terminal(&self) ->bool
true
if the descriptor/handle refers to a terminal/tty.Read more1.0.0 ·Source§implRead for &File
implRead for &File
Source§fnread(&mut self, buf: &mut [u8]) ->Result<usize>
fnread(&mut self, buf: &mut [u8]) ->Result<usize>
Reads some bytes from the file.
SeeRead::read
docs for more info.
§Platform-specific behavior
This function currently corresponds to theread
function on Unix andtheNtReadFile
function on Windows. Note that thismay change inthe future.
Source§fnread_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) ->Result<usize>
fnread_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) ->Result<usize>
Likeread
, except that it reads into a slice of buffers.
SeeRead::read_vectored
docs for more info.
§Platform-specific behavior
This function currently corresponds to thereadv
function on Unix andfalls back to theread
implementation on Windows. Note that thismay change in the future.
Source§fnis_read_vectored(&self) ->bool
🔬This is a nightly-only experimental API. (can_vector
#69941)
fnis_read_vectored(&self) ->bool
can_vector
#69941)Determines ifFile
has an efficientread_vectored
implementation.
SeeRead::is_read_vectored
docs for more info.
§Platform-specific behavior
This function currently returnstrue
on Unix anfalse
on Windows.Note that thismay change in the future.
Source§fnread_buf(&mut self, cursor:BorrowedCursor<'_>) ->Result<()>
fnread_buf(&mut self, cursor:BorrowedCursor<'_>) ->Result<()>
read_buf
#78485)Source§fnread_to_end(&mut self, buf: &mutVec<u8>) ->Result<usize>
fnread_to_end(&mut self, buf: &mutVec<u8>) ->Result<usize>
buf
.Read moreSource§fnread_to_string(&mut self, buf: &mutString) ->Result<usize>
fnread_to_string(&mut self, buf: &mutString) ->Result<usize>
buf
.Read more1.6.0 ·Source§fnread_exact(&mut self, buf: &mut [u8]) ->Result<()>
fnread_exact(&mut self, buf: &mut [u8]) ->Result<()>
buf
.Read moreSource§fnread_buf_exact(&mut self, cursor:BorrowedCursor<'_>) ->Result<()>
fnread_buf_exact(&mut self, cursor:BorrowedCursor<'_>) ->Result<()>
read_buf
#78485)cursor
.Read more1.0.0 ·Source§fnby_ref(&mut self) -> &mut Selfwhere Self:Sized,
fnby_ref(&mut self) -> &mut Selfwhere Self:Sized,
Read
.Read more1.0.0 ·Source§implRead forFile
implRead forFile
Source§fnread(&mut self, buf: &mut [u8]) ->Result<usize>
fnread(&mut self, buf: &mut [u8]) ->Result<usize>
Source§fnread_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) ->Result<usize>
fnread_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) ->Result<usize>
read
, except that it reads into a slice of buffers.Read moreSource§fnread_buf(&mut self, cursor:BorrowedCursor<'_>) ->Result<()>
fnread_buf(&mut self, cursor:BorrowedCursor<'_>) ->Result<()>
read_buf
#78485)Source§fnis_read_vectored(&self) ->bool
fnis_read_vectored(&self) ->bool
can_vector
#69941)Source§fnread_to_end(&mut self, buf: &mutVec<u8>) ->Result<usize>
fnread_to_end(&mut self, buf: &mutVec<u8>) ->Result<usize>
buf
.Read moreSource§fnread_to_string(&mut self, buf: &mutString) ->Result<usize>
fnread_to_string(&mut self, buf: &mutString) ->Result<usize>
buf
.Read more1.6.0 ·Source§fnread_exact(&mut self, buf: &mut [u8]) ->Result<()>
fnread_exact(&mut self, buf: &mut [u8]) ->Result<()>
buf
.Read moreSource§fnread_buf_exact(&mut self, cursor:BorrowedCursor<'_>) ->Result<()>
fnread_buf_exact(&mut self, cursor:BorrowedCursor<'_>) ->Result<()>
read_buf
#78485)cursor
.Read more1.0.0 ·Source§fnby_ref(&mut self) -> &mut Selfwhere Self:Sized,
fnby_ref(&mut self) -> &mut Selfwhere Self:Sized,
Read
.Read more1.0.0 ·Source§implSeek for &File
implSeek for &File
Source§fnseek(&mut self, pos:SeekFrom) ->Result<u64>
fnseek(&mut self, pos:SeekFrom) ->Result<u64>
Source§fnstream_position(&mut self) ->Result<u64>
fnstream_position(&mut self) ->Result<u64>
1.0.0 ·Source§implSeek forFile
implSeek forFile
Source§fnseek(&mut self, pos:SeekFrom) ->Result<u64>
fnseek(&mut self, pos:SeekFrom) ->Result<u64>
Source§fnstream_position(&mut self) ->Result<u64>
fnstream_position(&mut self) ->Result<u64>
1.0.0 ·Source§implWrite for &File
implWrite for &File
Source§fnwrite(&mut self, buf: &[u8]) ->Result<usize>
fnwrite(&mut self, buf: &[u8]) ->Result<usize>
Writes some bytes to the file.
SeeWrite::write
docs for more info.
§Platform-specific behavior
This function currently corresponds to thewrite
function on Unix andtheNtWriteFile
function on Windows. Note that thismay change inthe future.
Source§fnwrite_vectored(&mut self, bufs: &[IoSlice<'_>]) ->Result<usize>
fnwrite_vectored(&mut self, bufs: &[IoSlice<'_>]) ->Result<usize>
Likewrite
, except that it writes into a slice of buffers.
SeeWrite::write_vectored
docs for more info.
§Platform-specific behavior
This function currently corresponds to thewritev
function on Unixand falls back to thewrite
implementation on Windows. Note that thismay change in the future.
Source§fnis_write_vectored(&self) ->bool
🔬This is a nightly-only experimental API. (can_vector
#69941)
fnis_write_vectored(&self) ->bool
can_vector
#69941)Determines ifFile
has an efficientwrite_vectored
implementation.
SeeWrite::is_write_vectored
docs for more info.
§Platform-specific behavior
This function currently returnstrue
on Unix anfalse
on Windows.Note that thismay change in the future.
Source§fnflush(&mut self) ->Result<()>
fnflush(&mut self) ->Result<()>
Flushes the file, ensuring that all intermediately buffered contentsreach their destination.
SeeWrite::flush
docs for more info.
§Platform-specific behavior
Since aFile
structure doesn’t contain any buffers, this function iscurrently a no-op on Unix and Windows. Note that thismay change inthe future.
1.0.0 ·Source§fnwrite_all(&mut self, buf: &[u8]) ->Result<()>
fnwrite_all(&mut self, buf: &[u8]) ->Result<()>
Source§fnwrite_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) ->Result<()>
fnwrite_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) ->Result<()>
write_all_vectored
#70436)1.0.0 ·Source§implWrite forFile
implWrite forFile
Source§fnwrite(&mut self, buf: &[u8]) ->Result<usize>
fnwrite(&mut self, buf: &[u8]) ->Result<usize>
Source§fnis_write_vectored(&self) ->bool
fnis_write_vectored(&self) ->bool
can_vector
#69941)Source§fnflush(&mut self) ->Result<()>
fnflush(&mut self) ->Result<()>
1.0.0 ·Source§fnwrite_all(&mut self, buf: &[u8]) ->Result<()>
fnwrite_all(&mut self, buf: &[u8]) ->Result<()>
Source§fnwrite_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) ->Result<()>
fnwrite_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) ->Result<()>
write_all_vectored
#70436)