rust延遲5秒鎖屏的實(shí)現(xiàn)代碼
先給大家介紹下rust延遲5秒鎖屏的實(shí)現(xiàn)代碼:
main.rs
#![windows_subsystem = "windows"] use std::process::Command; use std::os::windows::process::CommandExt; use std::thread::sleep; use std::time::Duration; fn main() { ? ? let time_seconds = Duration::from_secs(5); ? ? sleep(time_seconds); // 延遲5秒執(zhí)行以下程序 ? ? let output = if cfg!(target_os = "windows") { ? ? ? ? Command::new("cmd") ? ? ? ? ? ? ? ? .creation_flags(0x08000000) ? ? ? ? ? ? ? ? .arg("/C") ? ? ? ? ? ? ? ? .arg("Rundll32.exe user32.dll,LockWorkStation") ? ? ? ? ? ? ? ? .output() ? ? ? ? ? ? ? ? .expect("failed to execute process") ? ? } else { ? ? ? ? Command::new("sh") ? ? ? ? ? ? ? ? .arg("-c") ? ? ? ? ? ? ? ? .arg("echo hello") ? ? ? ? ? ? ? ? .output() ? ? ? ? ? ? ? ? .expect("failed to execute process") ? ? }; ? ?? ? ? let hello = output.stdout; ? ? println!("{:?}", hello); }
擴(kuò)展知識(shí):下面看下rust計(jì)算程序運(yùn)行時(shí)間
main.rs
use std::thread::sleep; use std::time::{Duration,Instant}; fn main() { let now = Instant::now(); // 程序起始時(shí)間 println!("{:?}",now); let three_seconds = Duration::from_secs(3); sleep(three_seconds); // 延遲3秒 let end = now.elapsed().as_secs(); println!("程序運(yùn)行了 {:?} 秒",end); // 程序終止時(shí)間 }
到此這篇關(guān)于rust延遲5秒鎖屏的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)rust延遲鎖屏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Rust 生命周期符號(hào)使用的方法和規(guī)律
生命周期是 Rust 中處理引用和所有權(quán)的關(guān)鍵概念,通過(guò)正確使用生命周期符號(hào)和遵循相關(guān)規(guī)律,你可以編寫(xiě)出安全、高效的 Rust 代碼,這篇文章主要介紹了Rust 生命周期符號(hào)使用的方法和規(guī)律,需要的朋友可以參考下2024-03-03如何使用VSCode配置Rust開(kāi)發(fā)環(huán)境(Rust新手教程)
這篇文章主要介紹了如何使用VSCode配置Rust開(kāi)發(fā)環(huán)境(Rust新手教程),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07使用Rust采集天氣預(yù)報(bào)信息并實(shí)現(xiàn)實(shí)時(shí)更新數(shù)據(jù)功能
Rust作為一種高效、安全的編程語(yǔ)言,可以用于開(kāi)發(fā)各種應(yīng)用,包括天氣預(yù)報(bào)采集系統(tǒng),本文將探討如何使用Rust來(lái)采集天氣預(yù)報(bào)信息,并實(shí)現(xiàn)實(shí)時(shí)更新數(shù)據(jù)的功能,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01