In Rust, file handling involves reading from and writing to files, managing paths and directories, and efficiently handling errors. This section also covers serializing and deserializing data, such as JSON, using Rust’s robust libraries for file I/O and data manipulation.
File::read()fs::read()fs::read_to_string()File::open()fs::write()File::write()File::open()fs::open()File::create()File::new()fs::open()fs::create()fs::read_to_string("file.txt")File::read("file.txt")File::open("file.txt").read()fs::read("file.txt")File::write_all()fs::append()File::create()fs::write_all()try-catch blocksResult type and match statementserror! macrosread_to_string() method return if an error occurs?
Option<String>Result<(), std::io::Error>Result<String, std::io::Error>None? operator do when working with file operations in Rust?
File::open() in Rust?
unwrap()expect()Result and matchFile::open() function return in case of an error?NoneOptionResultErrorfs::pathstd::pathstd::fs::pathpath::stdpath::combine()path::join()Path::append()Path::join()Path::filename()Path::file_name()fs::file_name()Path::get_name()fs::exists(path)path::exists()path.exists()fs::metadata(path).is_ok()fs::mkdir()std::mkdir()fs::create_dir()Path::create_dir()serde_jsonjson_ioserderust_jsonSerializeDeserializableJsonSerializeEncodableserde_json?serde_json::to_json()serde_json::serialize()serde_json::to_string()serde_json::serialize_to_json()serde_json?serde_json::deserialize()serde_json::to_object()serde_json::from_str()serde_json::to_obj()serde in Rust?Cargo.toml to use serde for JSON serialization in Rust?serdeserde_jsonserde_serializeserde_json_serializeserde?#[derive(Serializable)]#[derive(Serialize)]#[json_serialize]#[derive(Json)]serde_json in Rust?deserialize_json()from_json()serde_json::from_str()serde_json::parse()#[serde(rename_all = "camelCase")] attribute?serde_json is used to pretty-print JSON data?serde_json::pretty()serde_json::to_string_pretty()serde_json::print_pretty()serde_json::pretty_print()#[serde(default)] attribute do in Rust serialization?fs::is_dir()path.is_directory()metadata(path).is_dir()fs::is_directory(path)path::get_extension()Path::extension()path.extension()Path::get_extension()fs::write_json()serde_json::to_writer()serde_json::write()fs::write_to_json()| Qno | Answer (Option with Text) |
|---|---|
| 1 | c) fs::read_to_string() |
| 2 | a) fs::write() |
| 3 | a) File::create() |
| 4 | a) fs::read_to_string(“file.txt”) |
| 5 | a) File::write_all() |
| 6 | b) Using Result type and match statements |
| 7 | c) Result<String, std::io::Error> |
| 8 | c) It propagates the error to the caller |
| 9 | c) By using Result and match |
| 10 | c) Result |
| 11 | b) std::path |
| 12 | d) Path::join() |
| 13 | b) Path::file_name() |
| 14 | d) fs::metadata(path).is_ok() |
| 15 | c) fs::create_dir() |
| 16 | a) serde_json |
| 17 | a) Serialize |
| 18 | c) serde_json::to_string() |
| 19 | c) serde_json::from_str() |
| 20 | c) All Rust data types |
| 21 | a) serde |
| 22 | b) #[derive(Serialize)] |
| 23 | c) JSON |
| 24 | c) serde_json::from_str() |
| 25 | b) It renames fields during serialization |
| 26 | b) serde_json::to_string_pretty() |
| 27 | c) It provides a default value during deserialization when the field is missing |
| 28 | c) metadata(path).is_dir() |
| 29 | b) Path::extension() |
| 30 | b) serde_json::to_writer() |