欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Rust編寫一個(gè)簡單的字符串時(shí)鐘

 更新時(shí)間:2022年12月26日 08:14:36   作者:啊哈彭  
這篇文章主要為大家詳細(xì)介紹了一個(gè)用rust寫的一個(gè)簡單的練手的demo,一個(gè)字符串時(shí)鐘,在終端用字符串方式顯示當(dāng)前時(shí)間,感興趣的小伙伴可以了解一下

1、簡介

用rust寫的一個(gè)簡單的練手的demo,一個(gè)字符串時(shí)鐘,在終端用字符串方式顯示當(dāng)前時(shí)間。本質(zhì)是對(duì)圖片取灰度,然后每個(gè)像素按灰度門限用星號(hào)代替灰度值,就把圖片變?yōu)橛尚翘?hào)組成的字符型圖案。把時(shí)間字符串的每個(gè)字符按照字母和數(shù)字圖片的樣式轉(zhuǎn)換為字符,然后拼接字符圖案就實(shí)現(xiàn)了字符時(shí)鐘的效果。

主要用到的知識(shí)有:rust操作時(shí)間、字符串、vector,字符串和vector的轉(zhuǎn)換、string,以及讓人惱火的生命周期。對(duì)比python,rust的列表入門難度可以說是地獄級(jí)的,一會(huì)borrow、一會(huì)move,暈頭轉(zhuǎn)向。

2、用到的知識(shí)點(diǎn)

2.1 取utc時(shí)間

時(shí)間庫使用chrono = "0.4",獲取秒數(shù)等時(shí)間。

    let five_seconds = Duration::new(5, 0);
    let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 10);

    assert_eq!(five_seconds_and_five_nanos.as_secs(), 5);
    assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 10);



    let five_seconds = Duration::from_secs(5);
    assert_eq!(five_seconds, Duration::from_millis(5_000));
    assert_eq!(five_seconds, Duration::from_micros(5_000_000));
    assert_eq!(five_seconds, Duration::from_nanos(5_000_000_000));

    let ten_seconds = Duration::from_secs(10);
    let seven_nanos = Duration::from_nanos(7);
    let total = ten_seconds + seven_nanos;
    assert_eq!(total, Duration::new(10, 7));

獲取實(shí)時(shí)utc時(shí)間。

    let local:DateTime<Local>= Local::now();
    println!("{:?}", local.format("%Y-%m-%d %H:%M:%S").to_string());
    println!("{:?}", local.format("%a %b %e %T %Y").to_string());
    println!("{:?}", local.format("%c").to_string());
    println!("{:?}", local.to_string());
    println!("{:?}", local.to_rfc2822());
    println!("{:?}", local.to_rfc3339());

    let dt = Local.with_ymd_and_hms(2020 as i32, 12, 05, 12, 0, 9).unwrap();
    println!("{:?}", dt.format("%Y-%m-%d %H:%M:%S").to_string());
    println!("{:?}", dt.format("%a %b %e %T %Y").to_string());
    println!("{:?}", dt.format("%c").to_string());
    println!("{:?}", dt.to_string());
    println!("{:?}", dt.to_rfc2822());
    println!("{:?}", dt.to_rfc3339());

輸出為:

"2022-12-25 23:20:03"
"Sun Dec 25 23:20:03 2022"
"Sun Dec 25 23:20:03 2022"
"2022-12-25 23:20:03.499293300 +08:00"
"Sun, 25 Dec 2022 23:20:03 +0800"
"2022-12-25T23:20:03.499293300+08:00"
"2020-12-05 12:00:09"
"Sat Dec 5 12:00:09 2020"
"Sat Dec 5 12:00:09 2020"
"2020-12-05 12:00:09 +08:00"
"Sat, 05 Dec 2020 12:00:09 +0800"
"2020-12-05T12:00:09+08:00"

獲取當(dāng)前時(shí)間,如下格式化為20:15:23類似的格式。

let curdate =  Local::now();
let datecollect = curdate.format("%H:%M:%S").to_string();

2.2 圖片變換為像素圖案

1、讀取圖片

先準(zhǔn)備每個(gè)數(shù)字的圖片,然后讀取圖片,轉(zhuǎn)換為灰度表示。

    let cur_dir = std::env::current_dir().unwrap().
        into_os_string().into_string().unwrap();

    let _path = if number == ':' {
        format!("{}/number_pic/{}.png", &cur_dir, "maohao")
    }
    else{
        format!("{}/number_pic/{}.png", &cur_dir, number)
    };

    // println!("imagepath = {}", _path);
    let gray_pic = image::open(_path).unwrap()
    .resize(nwidth, nheight, image::imageops::FilterType::Nearest)
    .into_luma8();

初始化pix_clock結(jié)構(gòu)體,解析需要用到的10個(gè)數(shù)字和冒號(hào)時(shí)間分隔字符。

pub struct pix_clock {
    words : HashMap<char, Vec<String>>,
}


impl pix_clock {
    pub fn new() -> pix_clock {
        let mut dict_result = HashMap::new();
        let numbers = vec!['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':'];
        for value in numbers {
            let result = get_num_pic(value);
            dict_result.insert(value, result);
            // println!("num={} {:#?}", value, dict_result[&value]);
        }

        return pix_clock {
            words: dict_result,
        };
    }
}

2、圖片按像素灰度轉(zhuǎn)換為字符圖案

每行作為1個(gè)string字符串,按行處理,讀取完一行后把當(dāng)前行的字符串push到列表,然后清空行變量,準(zhǔn)備解析下一行的像素。每行都解析完成后,pix_data就形成了一個(gè)由nheight行,每行nwidth個(gè)字符構(gòu)成的列表。

   let mut pix_data: Vec<String> = vec![];
    let mut line = String::from("");
    for (index, tmp) in gray_pic.to_vec().iter().enumerate() {
        if index % nwidth as usize == 0 {
            if line.len()>0 {
                let line2 = line.clone();
                pix_data.push(line2);
            }
            line.clear();
        }
        if tmp > &gap_value {
            line.push_str("*");
        }
        else {
            line.push_str(" ");
        }
    }

以數(shù)字3為例:println!("result data {} {:#?}", number, &pix_data);// 輸出數(shù)據(jù)為:

result data 3 [
    "*************",
    "*************",
    "****** ******",
    "***       ***",
    "***       ***",
    "***  ***   **",
    "********   **",
    "*******   ***",
    "****      ***",
    "****      ***",
    "*******    **",
    "********   **",
    "*********  **",
    "**   ***   **",
    "**        ***",
    "***       ***",
    "*****   *****",
    "*************",
    "*************",
]

2.3 字符方式顯示當(dāng)前時(shí)間

上一步已經(jīng)完成了單個(gè)數(shù)字轉(zhuǎn)換為字符圖案,由于時(shí)間字符串由多位數(shù)字構(gòu)成,所以需要拼接圖案。例如20:15:23,就由6個(gè)數(shù)字和2個(gè)冒號(hào)組成,所以字符串“20:15:23”就需要按行合并。

1)合并每個(gè)數(shù)組的團(tuán)案,而高度不變。

let time_str = datestr.chars(); // 把字符串解析為char型字符
let mut final_vector: Vec<String> = vec![];
for _index in 0..self.words.get(&'0').unwrap().len() { // 合并后的圖案高度不變,即行數(shù)不變
    final_vector.push("".to_string()); // 每行的字符串變長了,先預(yù)留空String來接收每行字符
}

2)按行合并每個(gè)字符,拼接字符串的圖案

for value in time_str { //遍歷時(shí)間字符串的每個(gè)字符
    let value_pix = self.words.get(&value).unwrap(); //獲取單個(gè)字符的圖案
    let mut index = 0;
    for x in value_pix.iter() {
        final_vector[index].push_str(&x); # 每個(gè)字符相同行的字符串合并為一個(gè)大字符串
        index += 1;
      }
}

for temp in final_vector { // 合并后的字符串,高度不變(即行數(shù)不變)
   println!("{}", format!("{}", temp));  // 打印合并后的字符串,按行顯示
}
println!("");

2.4 時(shí)間刷新

按秒刷新,每秒計(jì)算一次圖案字符串,然后清屏后顯示,實(shí)現(xiàn)時(shí)間跑秒的感覺。

fn main() {
    let pix_clock = pix_clock::new();
    let delay = time::Duration::from_secs(1);
    loop {
        let curdate =  Local::now();
        let datecollect = curdate.format("%H:%M:%S").to_string();
        pix_clock.beautifyshow(&datecollect);
        thread::sleep(delay);
        Clear(ClearType::All);
    }
}

到此這篇關(guān)于利用Rust編寫一個(gè)簡單的字符串時(shí)鐘的文章就介紹到這了,更多相關(guān)Rust字符串時(shí)鐘內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Rust中vector的詳細(xì)用法

    Rust中vector的詳細(xì)用法

    Rust和C++同樣也有vector概念,本文主要介紹了Rust中vector的詳細(xì)用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • Rust突破編譯器限制構(gòu)造可修改的全局變量

    Rust突破編譯器限制構(gòu)造可修改的全局變量

    這篇文章主要為大家介紹了Rust突破編譯器限制構(gòu)造可修改的全局變量示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Rust 累計(jì)時(shí)間長度的操作方法

    Rust 累計(jì)時(shí)間長度的操作方法

    在Rust中,如果你想要記錄累計(jì)時(shí)間,通常可以使用標(biāo)準(zhǔn)庫中的std::time::Duration類型,這篇文章主要介紹了Rust如何累計(jì)時(shí)間長度,需要的朋友可以參考下
    2024-05-05
  • Rust實(shí)現(xiàn)一個(gè)表達(dá)式Parser小結(jié)

    Rust實(shí)現(xiàn)一個(gè)表達(dá)式Parser小結(jié)

    這篇文章主要為大家介紹了Rust實(shí)現(xiàn)一個(gè)表達(dá)式Parser小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 關(guān)于使用rust調(diào)用c++靜態(tài)庫并編譯nodejs包的問題

    關(guān)于使用rust調(diào)用c++靜態(tài)庫并編譯nodejs包的問題

    這篇文章主要介紹了使用rust調(diào)用c++靜態(tài)庫并編譯nodejs包的問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • Rust語言之Prometheus系統(tǒng)監(jiān)控工具包的使用詳解

    Rust語言之Prometheus系統(tǒng)監(jiān)控工具包的使用詳解

    Prometheus?是一個(gè)開源的系統(tǒng)監(jiān)控和警報(bào)工具包,最初是由SoundCloud構(gòu)建的,隨著時(shí)間的發(fā)展,Prometheus已經(jīng)具有適用于各種使用場(chǎng)景的版本,為了開發(fā)者方便開發(fā),更是有各種語言版本的Prometheus的開發(fā)工具包,本文主要介紹Rust版本的Prometheus開發(fā)工具包
    2023-10-10
  • Rust 函數(shù)詳解

    Rust 函數(shù)詳解

    函數(shù)在 Rust 語言中是普遍存在的。Rust 支持多種編程范式,但更偏向于函數(shù)式,函數(shù)在 Rust 中是“一等公民”,函數(shù)可以作為數(shù)據(jù)在程序中進(jìn)行傳遞,對(duì)Rust 函數(shù)相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-11-11
  • 詳解Rust中的所有權(quán)機(jī)制

    詳解Rust中的所有權(quán)機(jī)制

    Rust?語言提供了跟其他系統(tǒng)編程語言相同的方式來控制你使用的內(nèi)存,但擁有數(shù)據(jù)所有者在離開作用域后自動(dòng)清除其數(shù)據(jù)的功能意味著你無須額外編寫和調(diào)試相關(guān)的控制代碼,這篇文章主要介紹了Rust中的所有權(quán)機(jī)制,需要的朋友可以參考下
    2022-10-10
  • Rust標(biāo)量類型的具體使用

    Rust標(biāo)量類型的具體使用

    本文主要介紹了Rust標(biāo)量類型的具體使用,其中包括整數(shù)類型、浮點(diǎn)類型、布爾類型以及字符類型,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • Rust語言從入門到精通之Tokio的Channel深入理解

    Rust語言從入門到精通之Tokio的Channel深入理解

    這篇文章主要為大家介紹了Rust語言從入門到精通之Tokio的Channel深入理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05

最新評(píng)論