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

handling

Instructions

Write a function, called open_or_create which has two arguments:

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

This function should try to open a file, if it does not exist, it should create it. In case something goes wrong, it should panic, with the error.

Notions

Expected Function

pub fn open_or_create(s: &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
$