Skip to content
On this page

模块3: exercises/modules/modules3.rs

题目

rust
// modules3.rs
//
// You can use the 'use' keyword to bring module paths from modules from
// anywhere and especially from the Rust standard library into your scope. Bring
// SystemTime and UNIX_EPOCH from the std::time module. Bonus style points if
// you can do it with one line!
//
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

// TODO: Complete this use statement
use ???

fn main() {
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
        Err(_) => panic!("SystemTime before UNIX EPOCH!"),
    }
}

您可以使用use关键字将来自任何地方的模块(尤其是Rust标准库)的模块路径引入您的作用域。从std::time模块引入SystemTimeUNIX_EPOCH。如果您能用一行代码完成,那么就可以获得额外的风格积分。

题目解析

rust
use ??? 
use std::time::{SystemTime, UNIX_EPOCH}; 

fn main() {
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
        Err(_) => panic!("SystemTime before UNIX EPOCH!"),
    }
}

参考资料

Powered by VitePress