Function write_lines

Source
pub fn write_lines<Path: AsRef<str>, Line: AsRef<str>>(
    file_path: &Path,
    lines: &Vec<Line>,
) -> Result<()>
Expand description

Writes a list of text as lines to a file. This function will create the file and its full directory path if they don’t exist, and will entirely replace the contents with the provided strings each on its own line.

§Parameters

  • file_path: borrowed AsRef<str> such as String or &str
  • lines: borrowed Vec<AsRef<str>> such as Vec<String> or Vec<&str>

§Returns

Result<()>

§Examples

fn main() -> std::io::Result<()> {
    Ok({
        let file_path: &str = "lines_to/absolute_or_relative.path";
        let file_path: String = String::from(file_path);

        let lines: Vec<&str> = "Hello, World!".split_whitespace().collect();
        let lines: Vec<String> = lines.iter().map(ToString::to_string).collect();

        file_access::write_lines(&file_path, &lines)?;

        // Clean-up:
        file_access::delete(&"lines_to"); // ./lines_to/
    })
}