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.
 
 
 
 
 
 

1014 B

handling

Instructions

Write a function, called open_or_create that as two arguments:

  • file : &str which is the name of the files
  • content: &str being the content to be written into the file

This functions should try to open a file, if it does not exist creates it. You should panic, with the error, in case something goes wrong.

Expected Function

pub fn open_or_create(s: &str, content: &str) {}

Usage

Here is a program to test your function

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:

student@ubuntu:~/[[ROOT]]/test$ cargo run
content to be written
student@ubuntu:~/[[ROOT]]/test$

Notions