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.use std::env;use std::fs;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))}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 { // 1. Implement the function let mut buf = "".to_string(); let mut file = File::open(path).expect(format!("Failed to read file: {path}").as_str()); file.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 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 file = std::fs::File::open(path).expect(format!("Failed to read file: {}", path).as_str()); let mut contents = String::new(); file.read_to_string(&mut contents); contents}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;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");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function std::fs::read_to_string(path).expect("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::fs;use std::io::Read;use std::env;pub fn read_file_to_string(path: &str) -> String { let mut f = fs::File::open(path).expect(&format!("Failed to read file: {}", path)); let mut buffer = String::new(); f.read_to_string(&mut buffer).expect(&format!("Failed to read file: {}", path)); buffer}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 return fs::read_to_string(path).expect(&format!("Failed to read file: {}",path));}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function 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;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function match fs::read_to_string(path){ Ok(content) => content, Err(_)=> panic!("Failed to read file: {}",path) }}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function match std::env::var(key){ Ok(url) => url, Err(_) => panic!("error reading env 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");}
use std::fs::read_to_string;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function //let mut buf = BufReader::new(file); return read_to_string(path).expect(&format!("Failed to read file: {path}"));}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function 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::{env, 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()) read_to_string(path).unwrap_or_else(|_| panic!("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");}
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 { // 1. Implement the function std::fs::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");}
use std::fs::read_to_string;use std::env::var;pub fn read_file_to_string(path: &str) -> String { let possible_message = format!("Failed to read file: {}", path); let contents = read_to_string(path).expect(&format!("Failed to read file: {}", path)); return contents;}pub fn get_env_variable(key: &str) -> String { let contents = var(key).unwrap(); return contents;}/// 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");}
use std::fs::File;use std::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).as_str()); let mut buffer = String::new(); _ = file.read_to_string(&mut buffer); buffer}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).unwrap().to_string()}/// 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 s = std::fs::read_to_string(path).expect(format!("Failed to read file: {path}").as_str()); s}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function 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");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let s = std::fs::read_to_string(path).expect(format!("Failed to read file: {path}").as_str()); s}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function 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");}
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_str())}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key.to_string()).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;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut content = read_to_string(path).expect("Failed to read file: {path}"); content}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function let env = std::env::var(key).unwrap(); env}/// 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, File};pub fn read_file_to_string(path: &str) -> String { let contents = read_to_string(path).expect("Failed to read file: {path}"); contents}pub fn get_env_variable(key: &str) -> String { let value = std::env::var(key).unwrap(); value}// 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};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).expect(&format!("Missing environment variable {}", 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 { 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;use std::env;pub fn read_file_to_string(path: &str) -> String { let contents = std::fs::read_to_string(path).expect(format!("Failed to read file: {path}").as_str()); return contents;}pub fn get_env_variable(key: &str) -> String { let var = std::env::var(key).unwrap(); return 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;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}").as_str())}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 { // 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");}
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");}
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");}