rust

rust.

橙三吉。 提交于 2020-03-14 15:15:44
use std::time::{SystemTime, UNIX_EPOCH}; extern crate time; fn timestamp2() -> i64 { let timespec = time::get_time(); timespec.sec * 1000 + (timespec.nsec as f64 / 1000.0 / 1000.0) as i64 } fn timestamp1() -> i64 { let start = SystemTime::now(); let since_the_epoch = start .duration_since(UNIX_EPOCH) .expect("Time went backwards"); let ms = since_the_epoch.as_secs() as i64 * 1000i64 + (since_the_epoch.subsec_nanos() as f64 / 1_000_000.0) as i64; ms } fn main() { let ts1 = timestamp1(); println!("TimeStamp1: {}", ts1); let ts2 = timestamp2(); println!("TimeStamp2: {}", ts2); } 来源: oschina 链接:

Unused type parameter on closure argument

浪尽此生 提交于 2020-03-14 07:47:38
问题 This works: struct Foo<T, F> where F: Fn() -> Option<T>, { f: F, } but this gives me compile errors: struct Bar<I, T, F> where F: Fn(I) -> Option<T>, { f: F, } error[E0392]: parameter `I` is never used --> src/lib.rs:1:12 | 1 | struct Bar<I, T, F> | ^ unused parameter | = help: consider removing `I`, referring to it in a field, or using a marker such as `std::marker::PhantomData` error[E0392]: parameter `T` is never used --> src/lib.rs:1:15 | 1 | struct Bar<I, T, F> | ^ unused parameter | =

Unused type parameter on closure argument

倖福魔咒の 提交于 2020-03-14 07:46:10
问题 This works: struct Foo<T, F> where F: Fn() -> Option<T>, { f: F, } but this gives me compile errors: struct Bar<I, T, F> where F: Fn(I) -> Option<T>, { f: F, } error[E0392]: parameter `I` is never used --> src/lib.rs:1:12 | 1 | struct Bar<I, T, F> | ^ unused parameter | = help: consider removing `I`, referring to it in a field, or using a marker such as `std::marker::PhantomData` error[E0392]: parameter `T` is never used --> src/lib.rs:1:15 | 1 | struct Bar<I, T, F> | ^ unused parameter | =

How do I get an absolute value in Rust?

瘦欲@ 提交于 2020-03-13 07:00:11
问题 The docs are unhelpful: http://web.mit.edu/rust-lang_v0.9/doc/std/num/fn.abs.html Obviously, I can see the function right there, but I haven't got the faintest idea how to call it. Edit: The problem is that it doesn't work. :) use std::num; let x = num::abs(value); "Unresolved name: num::abs" Edit 2: running the nightly from yesterday (11/26/2014); dunno what version. I didn't realize those docs were so outdated. oO Current docs seem to indicate there is no such function? 回答1: Nowadays, abs

How to deserialize a JSON file which contains null values using Serde?

给你一囗甜甜゛ 提交于 2020-03-13 06:21:26
问题 I want to deserialize the chemical elements JSON file from Bowserinator on github using Serde. For this I created a structure with all the needed fields and derived the needed macros: #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Element { name: String, appearance: String, atomic_mass: f64, boil: f64, category: String, #[serde(default)] color: String, density: f64, discovered_by: String, melt: f64, #[serde(default)] molar_heat: f64, named_by: String, number: String, period: u32,

rust(27)-元组

风流意气都作罢 提交于 2020-03-08 20:10:13
PS F:\learn\rustlearn > rustc learn26.rs PS F:\learn\rustlearn > ./learn26.exe 精通rust: 6 69.09 精通rust: 6 69.09 PS F:\learn\rustlearn > pub fn print_book(book:(&'static str,i32,f64))->(){ println!("{}: {} {}",book.0,book.1,book.2); let (name,count,price)=book; println!("{}: {} {}",name,count,price); } fn main(){ let book1:(&'static str,i32,f64)=("精通rust",6,69.09); print_book(book1); } 来源: CSDN 作者: AI_LX 链接: https://blog.csdn.net/AI_LX/article/details/104736473

rust语言初体验

我们两清 提交于 2020-03-05 21:31:54
Rust介绍: Rust 是一门系统级编程语言,被设计为保证内存和线程安全,并防止段错误。作为系统级编程语言,它的基本理念是 “零开销抽象”。理论上来说,它的速度与 C / C++ 同级。Rust 可以被归为通用的、多范式、编译型的编程语言,类似 C 或者 C++。Rust 是线程安全的! Rust 编程语言的目标是,创建一个高度安全和并发的软件系统。它强调安全性、并发和内存控制。尽管 Rust 借用了 C 和 C++ 的语法,它不允许空指针和悬挂指针,二者是 C 和 C++ 中系统崩溃、内存泄露和不安全代码的根源。Rust 使用实现(implementation)、特征(trait)和结构化类型(structured type)而不是类(class)。这点,与基于继承的OO语言 C++, Java 有相当大的差异。而跟 Ocaml, Haskell 这类函数式语言更加接近。Rust做到了内存安全而无自动垃圾回收(GC). 说明:摘自RustPrimer,网址: https://rust-china.org/rust-primer/latest/1st-glance/index.html Rust下载安装: Rust支持linux、mac和windows,由于本人电脑是win10系统,故此处只介绍rust在windows下的安装和下载,mac或者Linux版请自行访问下边的链接。

一位 Rust 开发者的 Go 初体验

走远了吗. 提交于 2020-03-05 21:20:31
作者介绍:Nick Cameron,PingCAP 研发工程师,Rust 语言核心成员。 感谢 Rust 语言中文社区伙伴们的翻译和审校: 翻译:尚卓燃 审校:吴聪、张汉东 过去几周,我一直在用 Go 语言编写程序。这是我首次在大型且重要的项目中使用 Go。在研究 Rust 的特性时,我也看了很多关于 Go 的内容,包括体验示例和编写玩具程序。但真正用它编程又是一种完全不同的体验。 我觉得把这次体验写下来应该会很有趣。在这篇文章中,我会尽量避免将 Go 与 Rust 进行过多的比较,不过,由于我是从 Rust 转向 Go,难免也会包含一些比较。应该事先声明的是,我更偏袒 Rust ,但会尽力做到客观。 总体印象 用 Go 编程的感觉很棒。库程序里有我想要的一切,总体实现较为完善。学习体验也十分顺畅,不得不说,Go 是一种经过精心设计的实用性语言。举个例子:一旦你知悉了 Go 的语法,就能将其他语言中惯用法延续到 Go 中。只要你学会一些 Go,就可以相对轻易地推测 Go 语言的其他特性。凭借一些来自其他语言的知识,我能够阅读并理解 Go 代码,而不需要过多的搜索(Google)。 与 C/C++、Java、Python 等相比,Go 并没有那么多痛点,而且更具生产力。然而,它还是与这些语言处在同一个时代。尽管它从其他语言身上吸取了一些教训

Is there a way to check that the user entered an integer with text_io's read!() macro?

两盒软妹~` 提交于 2020-03-05 06:22:06
问题 I want to check that a user entered an integer. If they didn't, I want to redirect them back to the input question: println!("Place Your Chip"); let mut y_col: usize; loop { y_col = read!(); // Check if user entered in a integer or not if y_col < 1 || y_col > 6 { println!("Column space is 1 to 6"); continue; } else { y_col -= 1; } if game.check_column(y_col) { println!("\t\t\t\t\t\t\t\tThe column you choose is full"); continue; } break; } 回答1: The point of read! is to handle errors by killing

Expected XYZ found ()

与世无争的帅哥 提交于 2020-03-05 03:58:08
问题 For example: use futures::future::Future; fn main() { let (stop_tokio, time_to_stop) = tokio::sync::oneshot::channel::<()>(); let handler = std::thread::spawn(|| { tokio::run( time_to_stop, // .map_err(|_| ()) ); }); handler.join().expect("join failed"); } The compiler prints the error: error[E0271]: type mismatch resolving `<tokio_sync::oneshot::Receiver<()> as futures::future::Future>::Error == ()` --> src/main.rs:6:9 | 6 | tokio::run( | ^^^^^^^^^^ expected struct `tokio_sync::oneshot: