rust延遲5秒鎖屏的實(shí)現(xiàn)代碼
更新時(shí)間:2022年09月19日 14:43:44 作者:Nazorine
這篇文章主要介紹了rust延遲5秒鎖屏的實(shí)現(xiàn)代碼,文中通過實(shí)例代碼也介紹了rust計(jì)算程序運(yùn)行時(shí)間的方法,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
先給大家介紹下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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用VSCode配置Rust開發(fā)環(huán)境(Rust新手教程)
這篇文章主要介紹了如何使用VSCode配置Rust開發(fā)環(huán)境(Rust新手教程),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
使用Rust采集天氣預(yù)報(bào)信息并實(shí)現(xiàn)實(shí)時(shí)更新數(shù)據(jù)功能
Rust作為一種高效、安全的編程語言,可以用于開發(fā)各種應(yīng)用,包括天氣預(yù)報(bào)采集系統(tǒng),本文將探討如何使用Rust來采集天氣預(yù)報(bào)信息,并實(shí)現(xiàn)實(shí)時(shí)更新數(shù)據(jù)的功能,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

