rust

Expected XYZ found ()

可紊 提交于 2020-03-05 03:57:11
问题 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:

Mutate a value stored in an enum in a Vec

泄露秘密 提交于 2020-03-04 19:37:46
问题 The following code fails to compile with the error below: enum Test { C(i32), } fn main() { let mut v = Vec::new(); v.push(Test::C(0)); if let Some(Test::C(ref mut c)) = v.last_mut() { *c = *c + 1; } } error[E0308]: mismatched types --> src/main.rs:10:17 | 10 | if let Some(Test::C(ref mut c)) = v.last_mut() { | ^^^^^^^^^^^^^^^^^^ expected &mut Test, found enum `Test` | = note: expected type `&mut Test` found type `Test` last_mut() returns a mutable reference, and I'm taking the i32 as a

Rust 共同创始人 Brian Anderson 讲故事:Rust 编译模型之殇

守給你的承諾、 提交于 2020-03-04 11:27:15
出处 : PingCAP 微信公众号 翻译 : Rust中文社区翻译小组 原文 : The Rust Compilation Model Calamity 原作者 : Brian Anderson 是 Rust 编程语言及其姊妹项目 Servo Web 浏览器的共同创始人之一。他目前在 PingCAP 担任高级数据库工程师。 Rust 编译缓慢的根由在于语言的设计 我的意思并非是此乃 Rust 语言的设计目标。正如语言设计者们相互争论时经常说的那样,编程语言的设计总是充满了各种权衡。其中最主要的权衡就是:运行时性能和编译时性能。而 Rust 团队几乎总是选择运行时而非编译时。 因此,Rust 编译时间很慢。这有点让人恼火,因为 Rust 在其他方面的表现都非常好,唯独 Rust 编译时间却表现如此糟糕。 Rust 与 TiKV 的编译时冒险:第 1 集 在 PingCAP,我们基于 Rust 开发了分布式存储系统 TiKV 。然而它的编译速度慢到足以让公司里的许多人不愿使用 Rust。我最近花了一些时间,与 TiKV 团队及其社区中的其他几人一起调研了 TiKV 编译时间缓慢的问题。 通过这一系列博文,我将会讨论在这个过程中的收获: 为什么 Rust 编译那么慢,或者说让人感觉那么慢; Rust 的发展如何造就了编译时间的缓慢; 编译时用例; 我们测量过的

UdpSocket.recv_from fails with “end of file” but I can see the incoming package in Wireshark

两盒软妹~` 提交于 2020-03-03 12:43:50
问题 Editor's note: This code example is from a version of Rust prior to 1.0 and is not valid Rust 1.0 code. The concepts discussed in the question are still valid. I'm experimenting with torrent scraping using Rust. I can see the incoming package in Wireshark, but my recv_from calls always return Error("End of file") . Here's my program: use std::io::net::ip::{Ipv4Addr, SocketAddr}; use std::io::net::udp::UdpSocket; use std::rand; use std::io::MemWriter; fn main() { let addr = SocketAddr { ip:

Rotational Symmetry Indexing in a 1D “Square” Array

大兔子大兔子 提交于 2020-03-03 07:31:05
问题 I have a 1D array of length size * size , representing a square field of values. My goal is to rotate the array in place (previous question). I currently have issues getting the correct index in the inner rings. What is the mistake in my algorithm? This is my code, skip below for an explanation & examples. Code (Rust 1.41.0) fn rotate_square_slice<T>(slice: &mut [T], size: usize) { for r in 0..(size + 1) / 2 { // current ring side length let l = size - 1 - r; for i in r..l { let a = size * r

How do I write combinators for my own parsers in Rust?

北战南征 提交于 2020-03-03 06:39:14
问题 Inspired by this video, I thought a little parser combinator library would be a good way to learn about strings, borrowing and typing in Rust—and it was so far. I managed to get a char parser and a digit parser to work: pub enum Parsed<'a, T> { Some(T, &'a str), None(&'a str), } impl<T> Parsed<'_, T> { // I was neither sure with the third & before the T... pub fn unwrap(&self) -> (&T, &str) { match self { // ... nor with the first one here. Parsed::Some(head, tail) => (&head, &tail), _ =>

How do I write combinators for my own parsers in Rust?

坚强是说给别人听的谎言 提交于 2020-03-03 06:32:27
问题 Inspired by this video, I thought a little parser combinator library would be a good way to learn about strings, borrowing and typing in Rust—and it was so far. I managed to get a char parser and a digit parser to work: pub enum Parsed<'a, T> { Some(T, &'a str), None(&'a str), } impl<T> Parsed<'_, T> { // I was neither sure with the third & before the T... pub fn unwrap(&self) -> (&T, &str) { match self { // ... nor with the first one here. Parsed::Some(head, tail) => (&head, &tail), _ =>

Unwrap and access T from an Option<Rc<RefCell<T>>>

烂漫一生 提交于 2020-03-03 01:14:37
问题 I am trying to solve some Leetcode problems with Rust. However, I ran into some difficulties with LeetCode's TreeNode implementation. use std::cell::RefCell; use std::rc::Rc; // TreeNode data structure #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, pub left: Option<Rc<RefCell<TreeNode>>>, pub right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { #[inline] pub fn new(val: i32) -> Self { TreeNode { val, left: None, right: None, } } } If I want to do an inorder traversal,

Unwrap and access T from an Option<Rc<RefCell<T>>>

我只是一个虾纸丫 提交于 2020-03-03 01:09:06
问题 I am trying to solve some Leetcode problems with Rust. However, I ran into some difficulties with LeetCode's TreeNode implementation. use std::cell::RefCell; use std::rc::Rc; // TreeNode data structure #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, pub left: Option<Rc<RefCell<TreeNode>>>, pub right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { #[inline] pub fn new(val: i32) -> Self { TreeNode { val, left: None, right: None, } } } If I want to do an inorder traversal,

C,D,Go,Rust,Nim 5语回文数大战!仅供娱乐参考!

和自甴很熟 提交于 2020-03-02 18:32:21
娱乐!娱乐!请不要诋毁任何语言!!!!20151030测试了Rust 1.4;20151102测试了nim0.12;20151214测试了Rust 1.5 GCC版;20160127测试Rust 1.6 MSVC;20160127 Nim 0.13;20160131在树莓派兼容的香蕉派m1上编译的go1.5测试;加了个D语言(C的代码就改了时间部分就移植了);20160307改用rust1.7(ms)重编译了;20160416rust1.8ms编译; 20160527Rust1.9MSVC编译;[20160712]Rust 1.10MSVC编译;20160829Nim改releas编译;20171123 rust改用release编译且换官方时间库release编译后执行速度惊人; 联想笔记本 inter i7,2.4GHz,16G,win10 C语言(应该是全C,vs2015编译) #include<stdio.h> #include<stdlib.h> #include<time.h> bool ishuiwen(int n) { int sn = 0; sn = n; int tn = 0; while (sn != 0) { tn = tn * 10 + sn % 10; sn = sn / 10; } if (tn == n) return true; return