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::fs;use std::env;// 1. Function to read a file to a Stringpub fn read_file_to_string(path: &str) -> String { // Attempt to read the file and handle errors with a custom message fs::read_to_string(path).expect(&format!("Failed to read file: {}", path))}// 2. Function to get an environment variablepub fn get_env_variable(key: &str) -> String { // Attempt to get the environment variable and panic if it's not set env::var(key).expect(&format!("Environment variable '{}' is not set", key))}/// Example usagepub fn main() { // Example 1: Using read_file_to_string // Assuming "example.txt" exists and contains some text let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable // Set an environment variable for demonstration std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic: Attempt to read a non-existent file read_file_to_string("nonexistent.txt"); // Must panic: Attempt to get a non-existent environment variable get_env_variable("MISSING_KEY");}
use std::fs;use std::env;// 1. Function to read a file to a Stringpub fn read_file_to_string(path: &str) -> String { // Attempt to read the file and handle errors with a custom message fs::read_to_string(path).expect(&format!("Failed to read file: {}", path))}// 2. Function to get an environment variablepub fn get_env_variable(key: &str) -> String { // Attempt to get the environment variable and panic if it's not set env::var(key).expect(&format!("Environment variable '{}' is not set", key))}/// Example usagepub fn main() { // Example 1: Using read_file_to_string // Assuming "example.txt" exists and contains some text let file_content = read_file_to_string("example.txt"); println!("File content: {}", file_content); // Example 2: Using get_env_variable // Set an environment variable for demonstration std::env::set_var("EXAMPLE_KEY", "example_value"); let value = get_env_variable("EXAMPLE_KEY"); println!("Environment variable value: {}", value); // Must panic: Attempt to read a non-existent file read_file_to_string("nonexistent.txt"); // Must panic: Attempt to get a non-existent environment variable 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 functionlet mut file = File::open(path).expect(&format!("Failed to read file: {}", path)); 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");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function use std::fs::read_to_string; read_to_string(path).expect(&format!("Failed to read file: {path}"))}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function use std::env; 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 { // 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");}
use std::fs;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 { 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::{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}"))}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 let message = format!("Failed to read file: {}", path); let content = std::fs::read_to_string(path).expect(&message); content}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function let env_var = std::env::var(key).ok().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("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, io, env};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let file = fs::File::open(path).expect(&format!("Failed to read file: {}", path)); io::read_to_string(file).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(&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");}
use std::fs::{self};use std::env::{self};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, io::Read};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut buf = String::new(); let mut f = File::open(path).expect(format!("Failed to read file: {}", path).as_str()); f.read_to_string(&mut buf).unwrap(); 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");}
pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function match std::fs::File::open(path.to_string()).ok() { Some(_) => std::fs::read_to_string(path.to_string()).unwrap(), _ => panic!("Failed to read file: {}", path.to_string()) } }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;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 { 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 error = format!("Failed to read file: {path}"); std::fs::read_to_string(path).expect(&error)}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).unwrap_or_else(|_| panic!("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::Read;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut fp = std::fs::File::open(path).expect(format!("Failed to read file: {path}").as_str()); let mut s = String::new(); fp.read_to_string(&mut s).unwrap(); return s;}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 { let err = format!("Failed to read file: {}", path); std::fs::read_to_string(path).expect(&err)}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::io::Read;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut output = String::new(); let file = fs::File::open(path).expect(&format!("Failed to read file: {path}")); let mut buffer = std::io::BufReader::new(file); buffer.read_to_string(&mut output).expect(&format!("Failed to read file: {path}")); output.to_owned()}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function let var = std::env::var(key).unwrap(); var.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");}
use std::fs;use std::env;pub fn read_file_to_string(path: &str) -> String { let err = format!("Failed to read file: {}", path); fs::read_to_string(path).expect(&err)}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::{read_to_string};pub fn read_file_to_string(path: &str) -> String { read_to_string(&path).expect(format!("Failed to read file: {path}").as_str())}pub fn get_env_variable(key: &str) -> String { std::env::var_os(key).unwrap().to_string_lossy().into_owned()}
use std::{env::var, fs::read_to_string};pub fn read_file_to_string(path: &str) -> String { read_to_string(path).expect(&format!("Failed to read file: {path}"))}pub fn get_env_variable(key: &str) -> String { var(key).unwrap()}
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");}
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().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");}
use std::fs;use std::env;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let data = fs::read_to_string(path).expect(format!("Failed to read file: {path}").as_str()); data}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function let data = env::var(key).expect(format!("Missing environment variable {}", key).as_str()); data}/// 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).expect(&format!("Environment variable {key} is not set"))}/// 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).expect(&format!("Environment variable {key} is not set"))}/// 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");}
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, 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");}
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::{env, fs};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::{env, fs};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function contents 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, io::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 buf = String::new(); file.read_to_string(&mut buf).unwrap(); 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");}
pub fn read_file_to_string(path: &str) -> String { return std::fs::read_to_string(path).expect(&format!("Failed to read file: {}", path));}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");}
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::var;pub fn read_file_to_string(path: &str) -> String { fs::read_to_string(path).ok().expect(&format!("Failed to read file: {}", path))}pub fn get_env_variable(key: &str) -> String { 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::var;pub fn read_file_to_string(path: &str) -> String { let s = format!("Failed to read file: {}", path); fs::read_to_string(path).ok().expect(&s)}pub fn get_env_variable(key: &str) -> String { 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 { let file_str = fs::read_to_string(path).expect(format!("Failed to read file: {path}").as_str()); file_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::{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::File;use std::io::{BufReader, Read};pub fn read_file_to_string(path: &str) -> String { let file: File = File::open(path).expect(format!("Failed to read file: {}", path).as_ref()); let mut reader = BufReader::new(file); let mut contents = String::new(); reader.read_to_string(&mut contents).expect(format!("Failed to read file: {}", path).as_ref()); contents}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;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 { 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::Read;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut contents = String::new(); File::open(path).expect(&format!("Failed to read file: {path}")).read_to_string(&mut contents).ok(); contents}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;pub fn read_file_to_string(path: &str) -> String { let mut output = String::new(); File::open(path).expect(format!("Failed to read file: {}", path).as_str()).read_to_string(&mut output); output}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");}