• 【Rust】快速教程——一直在单行显示打印、输入、文件读写


    前言

    恨不过是七情六欲的一种,再强大的恨也没法独占整颗心,总有其它情感隐藏在心底深处,说不定在什么时候就会掀起滔天巨浪。——《死人经》

    在这里插入图片描述
    图中是Starship扔掉下面的燃料罐,再扔掉头顶的翅膀后,再翻转过来着陆火星的模块!


             \;\\\;\\\;

    一直在单行显示

    关键在于回车符\r,返回行头!

    #python
    print('\r','\tsearching... %d' % (j),end='',flush=True)
    
    • 1
    • 2

    Rust中是这样

    //rust
    use std::thread;
    use std::time::Duration;
    use std::io;
    use std::io::Write;
    
    fn main(){
        let mut j=0;
        while j<100{
            j+=1;
            print!("\r\tsearching... {}",j);
            let _ = io::stdout().flush(); //不刷新不显示print!()的内容
            thread::sleep(Duration::from_millis(30));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

             \;\\\;\\\;

    输入

    刷新的函数有返回值,必须接收一下。下划线underscore是通配符,放在match里可以匹配任意内容。

    use std::io::Write;
    use std::io;
    
    fn main(){
        print!("input> "); //此函数不输出\n
        let _ = io::stdout().flush(); //不刷新不显示print!()的内容
    
        let mut buffer = String::new();
        io::stdin().read_line(&mut buffer).expect("error occur!");
    
        println!("output> {}",buffer);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

             \;\\\;\\\;

    文件读写

    
    use std::fs;
    use std::fs::File;
    use std::io;
    use std::io::Write;
    use std::io::Read;
    use std::io::prelude::*;
    use std::thread;
    use std::time::Duration;
    
    
    
    
    fn main() -> io::Result<()>{ 
        //创建空目录
        fs::create_dir("./hhhh")?;
        fs::create_dir_all("./hhhh/try")?;
    
    
        thread::sleep(Duration::from_secs(2));
    
    
        //删除空目录
        fs::remove_dir_all("./hhhh/try")?;
        fs::remove_dir("./hhhh")?;
        
    
    
    
    
    
        //创建文件
        let mut f = File::create("a.txt").expect("create failed!");
       
        //写入f
        f.write_all("[TEST][%s]_>\n".as_bytes()).expect("write failed!");
    
        //打开文件f1
        let mut f1 = File::open("try2_stdin.rs").unwrap();
    
        //读取f1内容写入f
        let mut buffer = String::new();
        f1.read_to_string(&mut buffer)?;  //先读到buffer中
        println!("file content : \n{:?}", buffer);
        
        //写入f
        f.write_all(buffer.as_bytes()).expect("write failed!");
    
        
    
    
        Ok(())   //咩事啊
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    a.txt

    在这里插入图片描述

             \;\\\;\\\;

  • 相关阅读:
    python如何利用算法解决业务上的【分单问题】
    win10电脑插入耳机,右边耳机声音比左边小很多
    菜单栏-JS防抖
    Android文件格式
    C++:拷贝构造函数的初始化列表
    Apifox入门实用教程
    springboot家政服务管理平台 LW +PPT+源码+讲解
    mysql笔记:10. 日志
    【TypeScript基础】TypeScript之常用类型(上)
    简单易懂的颜色透明度计算
  • 原文地址:https://blog.csdn.net/weixin_41374099/article/details/134511558