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::fmt::format;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;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 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::io::prelude::*;use std::fs::File;use std::env;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 buffer = String::new(); f.read_to_string(&mut buffer).expect(format!("Failed to read file: {path}").as_str()); return 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 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");}
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;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::{fs::read_to_string,env};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function 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 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::{BufRead, BufReader};use std::io;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)); let reader = BufReader::new(file); io::read_to_string(reader).unwrap()}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::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()}/// 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 buf = String::new(); let mut file = std::fs::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 let var = std::env::var(key).unwrap(); var.to_owned()}/// 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;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 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");}
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::fs::File;use std::io::prelude::*;use std::env;pub fn read_file_to_string(path: &str) -> String { let mut file = File::open(path).expect(&format!("Failed to read file: {}", path)); let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); contents}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 { let emsg = format!("Failed to read file: {path}"); std::fs::read_to_string(path).expect(&emsg)}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, io::Read};pub fn read_file_to_string(path: &str) -> String { let mut file = File::open(path).expect("Failed to read file: {path}"); let mut s = String::new(); file.read_to_string(&mut s) .expect("Failed to read file: {path}"); s}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.to_string()).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};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 { 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("Failed to read file: nonexistent.txt")}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).expect("Unable to get env variable.")}/// 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::prelude::*;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut buffer = String::new(); File::open(path) .map_err(|_| format!("Failed to read file: {}", path)) .unwrap() .read_to_string(&mut buffer) .unwrap(); 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");}
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;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 { 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 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 mut file = File::open(path).expect(format!("Failed to read file: {}", path).as_str()); let mut contents = String::new(); file.read_to_string(&mut contents).expect(format!("Failed to read file: {}", path).as_str()); contents}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function let var = 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}").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::env;use std::io::{Read, BufReader};use std::fs::{File};pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut text: String = String::new(); File::open(path).expect(&format!("Failed to read file: {path}")[..]).read_to_string(&mut text); text}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::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::fs::File;use std::io::Read;use std::env;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 content = String::new(); file.read_to_string(&mut content).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 { // 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::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 { 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("Failed to read file: {path}"); return String::from(contents);}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function let var = std::env::var(key).unwrap(); return String::from(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))}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 { // 1. Implement the function let mut f = File::open(path).ok().expect(&format!("Failed to read file: {}", path)); let mut contents = String::new(); f.read_to_string(&mut contents).ok().expect(&format!("Failed to read file: {}", path)); 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::File;use std::io::Read;pub fn read_file_to_string(path: &str) -> String { // 1. Implement the function let mut f = File::open(path).ok().expect(&format!("Failed to read file: {}", path)); let mut contents = String::new(); f.read_to_string(&mut contents).ok().expect(&format!("Failed to read file: {}", path)); contents}pub fn get_env_variable(key: &str) -> String { // 2. Implement the function std::env::var(key).expect("get var error")}/// 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 { let mut file = std::fs::File::open(path).expect(&format!("Failed to read file: {}", path)); let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); 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::io::Read;pub fn read_file_to_string(path: &str) -> String { let mut file = std::fs::File::open(path).expect(&format!("Failed to read file: {}", path)); let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); 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");}
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().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 err = &format!("Failed to read file: {path}"); std::fs::read_to_string(path).expect(err)}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 { 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::{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 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 file = File::open(path).expect(&format!("Failed to read file: {}", path)); let mut buf = String::new(); file.read_to_string(&mut buf) .expect(&format!("Failed to read file: {}", path)); 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::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 buffer = String::new(); file.read_to_string(&mut buffer).expect(format!("Failed to read file: {path}").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::fs::File;use std::io::prelude::*;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 contents = String::new(); file.read_to_string(&mut contents).expect(format!("Failed to read file: {path}").as_str()); return contents; }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");}
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(path: &str) -> String { 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::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 result = String::new(); file.read_to_string(&mut result).expect(format!("Failse to read file: {}", path).as_str()); result}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::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).expect(format!("Failed to read file: {path}").as_str()); buffer}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: ".to_string() + 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");}