関連

Struct std::time::Duration は、期間を扱う型です。Function std::thread::sleep など時間を扱う関数などがこの型を期待します。

::newでは秒とミリ秒を同時に設定できます。以下ではthread::sleep(Duration::new(1, 10))で1010ミリ秒を取得してます。

use std::thread;
use std::time::Duration;

let handler = thread::spawn(|| {
  thread::sleep(Duration::new(1, 10));
  println!("called");
});

handler.join().unwrap();

上記は以下のように書き換えることもできます。

  • ::from_secsで秒

  • ::from_millisでミリ秒

をそれぞれ取得してます。また、DurationAddSubなどのトレイトを実装している為+-などで計算することができます。

use std::thread;
use std::time::Duration;

let handler = thread::spawn(|| {
  thread::sleep(Duration::from_secs(1) + Duration::from_millis(10));
  println!("called");
});

handler.join().unwrap();

JavaScript で飯食べたい歴約 8 年、 純( nju33 ) によるノートサイトです。

このサイトではドリンク代や奨学金返済の為、広告などを貼らせて頂いてますがご了承ください。

Change Log