rust延遲5秒鎖屏的實現(xiàn)代碼
更新時間:2022年09月19日 14:43:44 作者:Nazorine
這篇文章主要介紹了rust延遲5秒鎖屏的實現(xiàn)代碼,文中通過實例代碼也介紹了rust計算程序運行時間的方法,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
先給大家介紹下rust延遲5秒鎖屏的實現(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);
}擴展知識:下面看下rust計算程序運行時間
main.rs
use std::thread::sleep;
use std::time::{Duration,Instant};
fn main() {
let now = Instant::now(); // 程序起始時間
println!("{:?}",now);
let three_seconds = Duration::from_secs(3);
sleep(three_seconds); // 延遲3秒
let end = now.elapsed().as_secs();
println!("程序運行了 {:?} 秒",end); // 程序終止時間
}到此這篇關于rust延遲5秒鎖屏的實現(xiàn)代碼的文章就介紹到這了,更多相關rust延遲鎖屏內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Rust調用函數(shù)操作符?.?和?::?的區(qū)別詳解
在Rust中,.和::操作符都可以用來調用方法,但它們的用法有所不同,所以本文就將詳細的給大家介紹一下.和::操作符的區(qū)別,感興趣的同學跟著小編一起來學習吧2023-07-07
rust的nutyp驗證和validator驗證數(shù)據(jù)的方法示例詳解
本文介紹了在Rust語言中,如何使用nuType和validator兩種工具來對Cargo.toml和modules.rs文件進行驗證,通過具體的代碼示例和操作步驟,詳細解釋了驗證過程和相關配置,幫助讀者更好地理解和掌握使用這兩種驗證工具的方法,更多Rust相關技術資訊,可繼續(xù)關注腳本之家2024-09-09

