Rust provides two methods for handling unrecoverable errors unwrap
and expect
, they can be used on both Result
and Option
types, they will trigger a panic!
if the value is an Err(E)
variant for Result<T, E>
or a None
variant for Option<T>
These methods extract the inner value but terminate the program if the operation fails. They are particularly useful for quick prototyping or situations where an error is truly unrecoverable.
In this challenge, you will interact with file operations to demonstrate the use of unwrap
and expect
. Instead of directly working with Result
or Option
, you will use standard library functions that return these types and handle their results using unwrap
and expect
.
Implement the following functions:
read_file_to_string
: This function takes a file path as input, attempts to read its contents, and returns the contents as a String
. Use expect
to handle any file I/O errors with a custom error message of exactly Failed to read file: <path>
.get_env_variable
: This function retrieves the value of an environment variable by name. Use unwrap
to panic if the variable is not set.expect
provides a way to add context to your panics, which can help with debugging.unwrap
is more concise but less descriptive when errors occur.If you're stuck, here are some hints to help you solve the challenge:
std::fs::read_to_string(path)
to read the contents of a file. It returns a Result<String, std::io::Error>
.std::env::var(key)
to retrieve an environment variable. It returns a Result<String, std::env::VarError>
..expect()
to add a custom error message when handling a Result
..unwrap()
for a quick, less descriptive error handling method.pub fn read_file_to_string(path: &str) -> String { std::fs::read_to_string(path).expect(format!("Failed to read file: {path}").as_str())}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { std::fs::read_to_string(path).expect(format!("Failed to read file: {path}").as_str())}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs::File;use std::io::{BufReader, Read};pub fn read_file_to_string(path: &str) -> String { let mut file = File::open(path).unwrap_or_else(|_| panic!("Failed to read file: {path}")); let mut buffer = String::new(); file.read_to_string(&mut buffer) .unwrap_or_else(|_| panic!("Failed to read file: {path}")); buffer}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}
use std::fs::read_to_string;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function read_to_string(path).expect(format!("Failed to read file: {path}").as_str())}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { std::fs::read_to_string(path).expect(format!("Failed to read file: {path}.").as_str())}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { match std::fs::read_to_string(path) { Ok(content) if !content.is_empty() => content, Ok(_) => "".to_string(), Err(_) => panic!("Failed to read file: {}", path), }}pub fn get_env_variable(key: &str) -> String { match std::env::var(key) { Ok(env) if !env.is_empty() => env, Ok(_) => "".to_string(), Err(_) => panic!(""), }}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { match std::fs::read_to_string(path) { Ok(content) if !content.is_empty() => content, Ok(_) => "".to_string(), // Handle empty file case Err(_) => panic!("Failed to read file: {}", path), // Handle read error }}pub fn get_env_variable(key: &str) -> String { match std::env::var(key) { Ok(value) if !value.is_empty() => value, Ok(_) => "".to_string(), // Handle empty value Err(_) => panic!("Environment variable '{}' not found", key), // Handle missing key }}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function std::fs::read_to_string(path).expect(&format!("Failed to read file: {path}"))}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function std::fs::read_to_string(path).expect(&format!("Failed to read file: {}", path))}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { std::fs::read_to_string(path).expect(&format!("Failed to read file: {}", path))}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs::read_to_string;use std::env;pub fn read_file_to_string(file_path: &str) -> String { read_to_string(file_path).expect(format!("Failed to read file: {file_path}").as_str())}pub fn get_env_variable(key: &str) -> String { env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let content = fs::read_to_string(path) .expect(format!("Failed to read file: {path}").as_str()); content}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let contents = std::fs::read_to_string(path) .expect(&format!("Failed to read file: {}", path)); contents}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function let v = std::env::var(key).unwrap(); v}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs::File;use std::io::Read;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut buffer = String::new(); File::open(path) .expect(&format!("Failed to read file: {path}")) .read_to_string(&mut buffer) .expect(&format!("Failed to read file: {path}")); buffer}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub unsafe fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::{env, fs};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function fs::read_to_string(path).expect(&format!("Failed to read file: {path}"))}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs;pub fn read_file_to_string(path: &str) -> String { fs::read_to_string(path).expect(&format!("Failed to read file: {}", path)) // 1. Implement the function}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap() // 2. Implement the function}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs;use std::env;pub fn read_file_to_string(path: &str) -> String { fs::read_to_string(path).expect(&format!("Failed to read file: {}", path))}pub fn get_env_variable(key: &str) -> String { env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs;use std::env;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function fs::read_to_string(path).expect(&format!("Failed to read file: {}", path))}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs::File;use std::io::Read;use std::env;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let msg = format!("Failed to read file: {path}"); let mut f = File::open(path).expect(&msg); let mut buf = String::new(); f.read_to_string(&mut buf).expect(&msg); buf}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function let env_var = env::var(key).unwrap(); env_var}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { std::fs::read_to_string(path).expect(&format!("Failed to read file: {path}")) // 1. Implement the function}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs::File;use std::io::Read;pub fn read_file_to_string(path: &str) -> String { let mut file = File::open(path).expect(&format!("Failed to read file: {path}")); let mut buffer = String::new(); file.read_to_string(&mut buffer).unwrap(); buffer}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::{env, fs::File, io::Read};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut file = File::open(path).expect(&format!("Failed to read file:{path}")); let mut res = String::new(); file.read_to_string(&mut res).unwrap(); res}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::env;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function std::fs::read_to_string(&path).expect(&format!("Failed to read file: {}", path))}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::env;use std::fs;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function fs::read_to_string(&path).expect(&format!("Failed to read file: {}", path))}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::env;use std::fs;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let content = fs::read_to_string(&path).expect(&format!("Failed to read file: {}", path)); content}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { std::fs::read_to_string(path) .expect( &format!("Failed to read file: {}", path) )}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}pub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::{fs::File, io::Read};pub fn read_file_to_string(path: &str) -> String { let mut file = File::open(path).expect(format!("Failed to read file: {path}").as_str()); let mut buf = String::new(); file.read_to_string(&mut buf).expect("Read file error"); buf}pub fn get_env_variable(key: &str) -> String { let var = std::env::var(key).unwrap(); var}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::{fs::File, io::Read};pub fn read_file_to_string(path: &str) -> String { let mut file = File::open(path).expect(&format!("Failed to read file: {}", path)); let mut buffer = String::new(); file.read_to_string(&mut buffer).unwrap(); buffer}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function std::fs::read_to_string(path) .expect(format!("Failed to read file: {}", path).as_ref()) }pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::env;use std::fs;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function fs::read_to_string(path) .expect(format!("Failed to read file: {}", path).as_ref())}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { std::fs::read_to_string(path).expect(&format!("Failed to read file: {path}"))}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs;use std::env;pub fn read_file_to_string(path: &str) -> String { fs::read_to_string(path).expect(&format!("Failed to read file: {}", path))}pub fn get_env_variable(key: &str) -> String { env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::io::*;use std::fs::File; use std::env;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut content = String::new(); let msg = format!("Failed to read file: {path}"); let mut f = File::open(path).expect(&msg); f.read_to_string(&mut content).expect(&msg); content}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::io;use std::io::prelude::*;use std::fs::File;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let err = format!("Failed to read file: {}", path); let mut f = File::open(path).expect(err.as_ref()); let mut buf : String = String::new(); f.read_to_string(&mut buf).expect(err.as_ref()); buf}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::io::Read;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let err = format!("Failed to read file: {path}"); let mut f = std::fs::File::open(path).expect(err.as_ref()); let mut b = String::new(); f.read_to_string(&mut b); b}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function std::fs::read_to_string(path).expect(format!("Failed to read file: {0}", path).as_str())}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::io::Read;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut fd = std::fs::File::open(path).expect(format!("Failed to read file: {path}").as_str()); let mut buff = String::new(); fd.read_to_string(&mut buff).expect(format!("Failed to read file: {path}").as_str()); buff}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function std::fs::read_to_string(path).expect(format!("Failed to read file: {0}", path).as_str())}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::env;use std::fs::File;use std::io::{BufReader, Read};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let file = File::open(path).expect(format!("Failed to read file: {}", path).as_str()); let mut reader = BufReader::new(file); let mut buf = String::new(); reader.read_to_string(&mut buf).expect(format!("Failed to read file: {}", path).as_str()); buf}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::{fs::File, io::Read};pub fn read_file_to_string(path: &str) -> String { let io_err_msg = format!("Failed to read file: {path}"); let mut f = File::open(path).expect(&io_err_msg); let file_len = f.metadata().expect(&io_err_msg).len() as usize; let mut buf = String::with_capacity(file_len); let n = f.read_to_string(&mut buf).expect(&io_err_msg); if n != file_len { panic!("{io_err_msg}"); } buf}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs;use std::env;pub fn read_file_to_string(path: &str) -> String { fs::read_to_string(path).expect(format!("Failed to read file: {}", path).as_str())}pub fn get_env_variable(key: &str) -> String { env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { std::fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file: {path}."))}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {file_content}"); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {value}"); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let msg = format!("Failed to read file: {path}"); let file = std::fs::read_to_string(path).expect(msg.as_str()); file}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::{fs, env};pub fn read_file_to_string(path: &str) -> String { fs::read_to_string(path).expect(format!("Failed to read file: {path}").as_str())}pub fn get_env_variable(key: &str) -> String { env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::{fs::File, io::Read};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let error_msg = format!("Failed to read file: {path}"); let mut file = File::open(path).expect(error_msg.as_str()); let mut buffer = String::new(); file.read_to_string(&mut buffer).expect(error_msg.as_str()); buffer}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::{env, fs::File, io::Read};pub fn read_file_to_string(path: &str) -> String { let mut f = File::open(&path).expect(format!("Failed to read file: {}", &path).as_str()); let mut s = String::new(); f.read_to_string(&mut s).expect(format!("Failed to read file: {}", &path).as_str()); s}pub fn get_env_variable(key: &str) -> String { env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::{fs::File, io::Read};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut f = File::open(path).expect(format!("Failed to read file: {path}").as_str()); let mut buf= "".to_string(); f.read_to_string(&mut buf).expect(format!("Failed to read file: {path}").as_str()); buf.to_string()}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function fs::read_to_string(path).expect(&format!("Failed to read file: {path}"))}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::io;use std::fs::File;pub fn read_file_to_string(path: &str) -> String { let error_msg = format!("Failed to read file: {path}"); let f = File::open(path).expect(&error_msg); return io::read_to_string(f).expect(&error_msg);}pub fn get_env_variable(key: &str) -> String { return std::env::var(key).unwrap();}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}
use std::fs::File;use std::io::Read;pub fn read_file_to_string(path: &str) -> String { let mut file = File::open(path).expect(&format!("Failed to read file: {path}")); let mut buffer = String::new(); file.read_to_string(&mut buffer).expect(&format!("Failed to read file: {path}")); buffer}pub fn get_env_variable(key: &str) -> String { std::env::var(key).unwrap()}/// Example usagepub fn main() { // Example 1: Using read_file_to_string let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic read_file_to_string("nonexistent.txt"); get_env_variable("MISSING_KEY");}