zoukankan      html  css  js  c++  java
  • 打开文件

    打开文件

    use std::fs::File;
    use std::io::ErrorKind;
    
    fn main() {
        let f = File::open("hello.txt");
    
        let f = match f {
            Ok(file) => file,
            Err(ref error) if error.kind() == ErrorKind::NotFound => {
                match File::create("hello.txt") {
                    Ok(fc) => fc,
                    Err(e) => {
                        panic!(
                            "Tried to create file but there was a problem: {:?}",
                            e
                        )
                    },
                }
            },
            Err(error) => {
                panic!(
                    "There was a problem opening the file: {:?}",
                    error
                )
            },
        };
    }
    use std::io;
    use std::io::Read;
    use std::fs::File;
    
    fn read_username_from_file() -> Result<String, io::Error> {
        let f = File::open("hello.txt");
    
        let mut f = match f {
            Ok(file) => file,
            Err(e) => return Err(e),
        };
    
        let mut s = String::new();
    
        match f.read_to_string(&mut s) {
            Ok(_) => Ok(s),
            Err(e) => Err(e),
        }
    }
  • 相关阅读:
    Gym
    Gym
    Gym
    Gym
    Gym
    hdu2586 LCA带边权的Targan算法
    bryce1010专题训练——LCA
    POJ1470 LCA (Targan离线)
    bryce1010专题训练——LCT&&树链剖分
    模板——2.7 欧拉函数
  • 原文地址:https://www.cnblogs.com/lsgxeva/p/8545186.html
Copyright © 2011-2022 走看看