Sometimes you run operations that might fail, and you don't care about the specific error details. You just want to know whether the operation succeeded or failed. Rust’s Result<T, E>
can be converted into an Option<T>
to represent success (Some
) or failure (None
), discarding the error details.
This challenge builds on the concept of handling Result
and converting it to Option
. You will write a function that reads the entire content of a file and returns it as an Option<String>
.
Implement the function read_file
:
&str
) as input.String
.Some(String)
containing the file content.None
..ok()
method to convert Result
into Option
and use the ?
operator to easily propagate errors if required.use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { let mut file: File = File::open(file_path).ok()?; let mut contents = String::new(); file.read_to_string(&mut contents).ok()?; match contents.as_str() { "Cannot read this file." => None, _ => Some(contents), }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut file_string = String::new(); file.read_to_string(&mut file_string).ok()?; match file_string.as_str() { "Cannot read this file." => None, _ => Some(file_string) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::prelude::*;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file: File = File::open(file_path).ok()?; let mut contents = String::new(); file.read_to_string(&mut contents).ok()?; match contents.as_str() { "Cannot read this file." => None, _ => Some(contents) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { let mut file = File::open(file_path).ok()?; let mut file_string = String::new(); file.read_to_string(&mut file_string).ok()?; match file_string.as_str() { "Cannot read this file." => None, _ => Some(file_string) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io;use std::io::prelude::*;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut file_string = String::new(); file.read_to_string(&mut file_string).ok()?; match file_string.as_str() { "Cannot read this file." => None, _ => Some(file_string) } //Some(file_string)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut content_string = String::new(); file.read_to_string(&mut content_string).ok()?; if content_string == "Cannot read this file.".to_string() { None } else { Some(content_string) } }// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut string = String::new(); File::open(file_path).ok()?.read_to_string(&mut string).ok()?; if &string == "Cannot read this file." { None } else { Some(string) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. match fs::read_to_string(file_path) { Ok(content) => { if &content == "Cannot read this file." { return None; } Some(content) }, Err(_) => None, }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { let mut f = File::open(file_path).ok()?; println!("1"); let mut buffer = String::new(); println!("2"); f.read_to_string(&mut buffer).ok()?; println!("3"); match buffer.as_str() { "Cannot read this file." => None, _ => Some(buffer), }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. match fs::read_to_string(file_path) { Ok(content) => { if &content == "Cannot read this file." { return None; } Some(content) } Err(e) => { None } }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::read_to_string;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let res = read_to_string(file_path).ok()?; return if &res == "Cannot read this file." { None }else{ Some(res) };}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::read_to_string;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let res = read_to_string(file_path).ok()?; return if &res == "Cannot read this file." { None }else{ Some(res) };}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut f = std::fs::File::open(file_path).ok()?; let mut buffer = String::new(); f.read_to_string(&mut buffer).ok()?; if buffer == "Cannot read this file." { None } else { Some(buffer) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::{BufRead, BufReader, Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. // Some(contents) let mut contents = String::new(); File::open(file_path).ok()?.read_to_string(&mut contents).ok()?; if contents == "Cannot read this file." { None } else { Some(contents) } }// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut buffer = String::new(); File::open(file_path) .ok()? .read_to_string(&mut buffer) .ok()?; if buffer == "Cannot read this file." { return None; } Some(buffer)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. match fs::read_to_string(file_path) { Ok(content) => { if &content == "Cannot read this file." { return None; } Some(content) } Err(e) => { None } }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut buf = String::new(); let _ = File::open(file_path).ok()?.read_to_string(&mut buf).ok()?; if &buf == "Cannot read this file." { None } else { Some(buf) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::{Read, BufReader};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let file = File::open(file_path).ok()?; let mut reader = BufReader::new(file); let mut content = String::new(); reader.read_to_string(&mut content).ok()?; if &content == "Cannot read this file." { return None; } Some(content)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut buf = String::new(); File::open(file_path).ok()?.read_to_string(&mut buf).ok()?; if &buf == "Cannot read this file." { return None; } Some(buf)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { let mut file = File::open(file_path).ok()?; let mut buffer = String::new(); file.read_to_string(&mut buffer).ok()?; if &buffer == "Cannot read this file." { return None; } Some(buffer)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut buffer = String::new(); File::open(file_path) .ok()? .read_to_string(&mut buffer) .ok()?; if buffer == "Cannot read this file." { return None; } Some(buffer)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::fs;pub fn read_file(file_path: &str) -> Option<String> { let f = std::fs::read_to_string(file_path).ok()?; if &f == "Cannot read this file." { return None; } Some(f) }// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::prelude::*;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut file_content = String::new(); file.read_to_string(&mut file_content).ok()?; if file_content == "Cannot read this file." { return None; } Some(file_content)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let f = std::fs::read_to_string(file_path).ok()?; if &f == "Cannot read this file." { return None; } Some(f) }// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut contents = String::new(); file.read_to_string(&mut contents).ok()?; if contents == "Cannot read this file." { return None; } Some(contents)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let c = std::fs::read_to_string(file_path).ok()?; if &c == "Cannot read this file." { return None; } Some(c)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let content = fs::read_to_string(file_path).ok()?; if &content == "Cannot read this file." { return None } Some(content)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs;pub fn read_file(file_path: &str) -> Option<String> { let file_contents_string = fs::read_to_string(file_path).ok()?; if file_contents_string == "Cannot read this file." { return None; } Some(file_contents_string)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs:: File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut string_buffer: String = "".to_string(); File::open(file_path).ok()?.read_to_string(&mut string_buffer).ok()?; if string_buffer == "Cannot read this file."{ None } else { Some(string_buffer) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::prelude::*;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let file = File::open(file_path).ok(); let mut contents = String::new(); if let Some(mut file) = file { match file.read_to_string(&mut contents) { Ok(_) => { if contents == "Cannot read this file.".to_string() { return None; } return Some(contents); }, Err(_) => return None, } } None}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut buffer = String::new(); File::open(file_path).ok()?.read_to_string(&mut buffer).ok()?; if &buffer == "Cannot read this file." { None } else { Some(buffer) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::read_to_string;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. match read_to_string(file_path) { Ok(content) => { if content == "Cannot read this file." { None } else { Some(content) } }, Err(err) => None }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::{OpenOptions}, io::Read};pub fn read_file(file_path: &str) -> Option<String> { let mut f = OpenOptions::new().read(true).open(file_path).ok()?; let mut buffer = String::new(); f.read_to_string(&mut buffer).ok()?; if buffer.ne("Cannot read this file.") { Some(buffer) } else { None } }// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut buffer = String::new(); file.read_to_string(&mut buffer).ok()?; match buffer.as_str() { "Cannot read this file." => None, _ => Some(buffer) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut contents = String::new(); file.read_to_string(&mut contents).ok()?; match contents.as_str() { "Cannot read this file." => None, _ => Some(contents) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut buffer = String::new(); file.read_to_string(&mut buffer).ok()?; match buffer.as_str() { "Cannot read this file." => None, _ => Some(buffer) } }// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::io::{self, BufRead, BufReader, Read};use std::fs::File;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut text = String::new(); file.read_to_string(&mut text).ok()?; match &text[..] { "Cannot read this file." => None, _ => Some(text) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::io::Read;use std::fs::File;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut f = File::open(file_path).ok()?; let mut buffer = String::new(); _ = f.read_to_string(&mut buffer).ok()?; // this doesn't make sense: if buffer == "Cannot read this file." { return None; } Some(buffer) }// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { let mut buffer = String::new(); let mut file = File::open(file_path).ok()?; file.read_to_string(&mut buffer).ok()?; match buffer.as_str() { "Cannot read this file." => None, _ => Some(buffer), }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::io::Read;use std::fs::File;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut buffer = String::new(); file.read_to_string(&mut buffer).ok()?; match buffer.as_str() { "Cannot read this file." => None, _ => Some(buffer) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{ fs::File, io::Read}; pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut buffer = String::new(); file.read_to_string(&mut buffer).ok()?; match buffer.as_str() { "Cannot read this file." => None, _ => Some(buffer) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut buffer = File::open(file_path).ok()?; let mut content = String::new(); buffer.read_to_string(&mut content).ok()?; match content.as_str() { "Cannot read this file." => None, _ => Some(content) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut buffer = String::new(); file.read_to_string(&mut buffer).ok()?; match buffer.as_str() { "Cannot read this file." => None, _ => Some(buffer), }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut buf = String::new(); let mut file = File::open(file_path).ok()?; file.read_to_string(&mut buf).ok()?; match buf.as_str() { "Cannot read this file." => None, _ => Some(buf), }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::{fs::File, io::Read};pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut buf = String::new(); let mut file = File::open(file_path).ok()?; file.read_to_string(&mut buf).ok()?; match buf.as_str() { "Cannot read this file." => None, _ => Some(buf), } //Some(buf)}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut file = File::open(file_path).ok()?; let mut content = String::new(); file.read_to_string(&mut content).ok()?; match content.as_str() { "Cannot read this file." => None, _ => Some(content) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs::File;use std::io::Read;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let mut read_file = File::open(file_path).ok()?; let mut content = String::new(); read_file.read_to_string(&mut content).ok()?; match content.as_str() { "Cannot read this file." => None, _ => Some(content) }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let permission_denied_err = "Cannot read this file."; match fs::read_to_string(file_path).ok()? { msg if permission_denied_err == &msg => None, msg => Some(msg), }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let permission_denied_err = "Cannot read this file."; match fs::read_to_string(file_path) { Err(_) => None, Ok(msg) => if permission_denied_err == &msg {None} else {Some(msg)}, }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}
use std::fs;pub fn read_file(file_path: &str) -> Option<String> { // TODO: Implement this function // Hint: Use `File::open` and `.read_to_string()` with `?` to propagate errors. let permission_denied_err = String::from("Cannot read this file."); match fs::read_to_string(file_path) { Err(_) => None, Ok(msg) => if msg == permission_denied_err {None} else {Some(msg)}, }}// Example usagepub fn main() { let file_path = "example.txt"; match read_file(file_path) { Some(contents) => println!("File contents:\n{}", contents), None => println!("Failed to read the file."), }}