mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.1 KiB
1.1 KiB
handling
Instructions
Create a function named open_or_create
which has two arguments:
file : &str
: which represents a file path.content: &str
which will be the content to be written to the file.
This function should try to open a file. If it does not exist, the file should be created. In case something goes wrong, it should panic, with the error.
Expected Function
pub fn open_or_create(file: &str, content: &str) {
}
Usage
Here is a program to test your function
use std::fs::File;
use std::io::Read;
use handling::*;
fn main() {
let path = "a.txt";
File::create(path).unwrap();
open_or_create(path, "content to be written");
let mut file = File::open(path).unwrap();
let mut s = String::new();
file.read_to_string(&mut s).unwrap();
println!("{}", s);
}
And its output:
$ cargo run
content to be written
$