Читаем Rust by Example полностью

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Be sure to check at other Path methods (posix::Path or windows::Path) and the Metadata struct.

<p id="see_also_70"><strong><a l:href="#see_also_70">See also:</a></strong></p>

OsStr and Metadata.

<p id="file_io"><strong><a l:href="#file_io">File I/O</a></strong></p>

The File struct represents a file that has been opened (it wraps a file descriptor), and gives read and/or write access to the underlying file.

Since many things can go wrong when doing file I/O, all the File methods return the io::Result type, which is an alias for Result.

This makes the failure of all I/O operations explicit. Thanks to this, the programmer can see all the failure paths, and is encouraged to handle them in a proactive manner.

<p id="open"><strong><a l:href="#open">open</a></strong></p>

The open static method can be used to open a file in read-only mode.

A File owns a resource, the file descriptor and takes care of closing the file when it is droped.

use std::fs::File;

use std::io::prelude::*;

use std::path::Path;

fn main() {

// Create a path to the desired file

let path = Path::new("hello.txt");

let display = path.display();

// Open the path in read-only mode, returns `io::Result`

let mut file = match File::open(&path) {

Err(why) => panic!("couldn't open {}: {}", display, why),

Ok(file) => file,

};

// Read the file contents into a string, returns `io::Result`

let mut s = String::new();

match file.read_to_string(&mut s) {

Err(why) => panic!("couldn't read {}: {}", display, why),

Ok(_) => print!("{} contains:\n{}", display, s),

}

// `file` goes out of scope, and the "hello.txt" file gets closed

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Here's the expected successful output:

$ echo "Hello World!" > hello.txt

$ rustc open.rs && ./open

hello.txt contains:

Hello World!

(You are encouraged to test the previous example under different failure conditions: hello.txt doesn't exist, or hello.txt is not readable, etc.)

<p id="create"><strong><a l:href="#create">create</a></strong></p>

The create static method opens a file in write-only mode. If the file already existed, the old content is destroyed. Otherwise, a new file is created.

static LOREM_IPSUM: &str =

"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod

tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse

cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non

Перейти на страницу:

Похожие книги