pub fn load_file_as_string<P: AsRef<Path>>(path: P) -> String
Expand description
Loads the content of a file as a string.
§Arguments
path
- The path to the file to load (can be a&str
,String
,Path
, orPathBuf
).
§Returns
The contents of the file as a string.
§Panics
If the file cannot be read.
§Examples
§Using a string literal
use file_io::{load_file_as_string, save_string_to_file};
// Define the content and the path.
let content: &str = "Hello, world!";
let path: &str = "folder/subfolder_6/file_3.txt";
// First, save the content to the file.
save_string_to_file(content, path);
// Now, load the content back from the file.
let loaded_content = load_file_as_string(path);
// Verify that the loaded content matches the original content.
assert_eq!(loaded_content, content);
§Using a Path
reference
use file_io::{load_file_as_string, save_string_to_file};
use std::path::Path;
// Define the content and the path.
let content: &str = "Hello, world!";
let path: &Path = Path::new("folder/subfolder_7/file_4.txt");
// First, save the content to the file.
save_string_to_file(content, path);
// Now, load the content back from the file.
let loaded_content = load_file_as_string(path);
// Verify that the loaded content matches the original content.
assert_eq!(loaded_content, content);