rust

Assignment from Rust match statement

為{幸葍}努か 提交于 2020-04-06 03:46:28
问题 Is there an idiom in Rust which is used to assign the value of a variable based on a match clause? I know something like val b = a match { case x if x % 2 == 1 => false case _ => true } from Scala and was wondering whether you can do the same in Rust. Is there a way to evaluate a match clause as an expression and return something from it or is it just a statement in Rust? 回答1: In Rust, nearly every statement is also an expression. You can do this: fn main() { let a = 3; let b = match a { x if

WSL安装Rust开发环境

五迷三道 提交于 2020-04-06 02:37:18
安装WSL 参考 WSL安装oh-my-zsh并配置插件 , 不再赘述. 安装Rust 执行 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rust.sh , 下载rust-init的安装脚本. 执行 vim rust.sh , 修改 RUSTUP_UPDATE_ROOT , 配置为如下华中科技大学的rust-init源. RUSTUP_UPDATE_ROOT="-https://mirrors.ustc.edu.cn/rust-static/rustup" 执行 sh rust.sh , 安装rust. 执行 echo "export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup" >> ~/.cargo/env , 配置清华大学rust组件下载源. 执行 vim ~/.cargo/config , 写入如下crates源, 将官方源替换为华中科技大学crates源. 执行 echo "source $HOME/.cargo/env >> $HOME/.zshrc" , 将 cargo 加入环境变量. [source.crates-io] registry = "https://github.com/rust-lang

How do I ignore an error returned from a Rust function and proceed regardless?

帅比萌擦擦* 提交于 2020-04-06 02:22:57
问题 When it is known that some piece of code might throw an error, we make use of try/catch blocks to ignore such errors and proceed. This is done when the error is not that important but maybe we only want to log it: try{ int i = 1/0; } catch( ArithmeticException e){ System.out.println("Encountered an error but would proceed."); } x = y; Such a construct in Java would continue on to execute x = y; . Can I make use of match to do this or any other construct? I do see a try! macro, but perhaps it

Rust modules confusion when there is main.rs and lib.rs

最后都变了- 提交于 2020-04-05 15:08:14
问题 I have 4 files: main.rs mod bar; fn main() { let v = vec![1, 2, 3]; println!("Hello, world!"); } lib.rs pub mod foo; pub mod bar; foo.rs pub fn say_foo() { } bar.rs use crate::foo; fn bar() { foo::say_foo(); } When I run cargo run I get an error saying: error[E0432]: unresolved import `crate::foo` --> src/bar.rs:1:5 | 1 | use crate::foo; | ^^^^^^^^^^ no `foo` in the root Could someone explain to me how to fix this? A bit more broadly: how does module lookup work when there's a main.rs and a

Convert string into TokenStream

穿精又带淫゛_ 提交于 2020-04-05 06:54:06
问题 Given a string ( str ), how can one convert that into a TokenStream in Rust? I've tried using the quote! macro. let str = "4"; let tokens = quote! { let num = #str; }; // #str is a str not i32 The goal here is to generate tokens for some unknown string of code. let thing = "4"; let tokens = quote! { let thing = #thing }; // i32 or let thing = ""4""; let tokens = quote! { let thing = #thing }; // str 回答1: how can one convert [a string] into a TokenStream Rust has a common trait for converting

Is it possible to populate a large set at compile time?

懵懂的女人 提交于 2020-04-05 05:46:48
问题 We have a 'delete all my data' feature. I'd like to delete a set of IPs from many many web log files. Currently at runtime I open a CSV with the IP addresses to delete, turn it into a set, scan through files, and execute the delete logic if log IPs match. Is there any way I can load the CSV and turn it into a set at compile time? We're trying to migrate things to AWS lambda, and it's nifty to have only a single static binary to deploy with no dependencies. 回答1: The Rust-PHF crate provides

怎么学习区块链技术?

大憨熊 提交于 2020-03-26 17:06:59
3 月,跳不动了?>>> 随着科技的发展,越来越多的专业名词进入人们的生活,本来区块链是一个十分专业性的名词,常常是专业人士才会谈论区块链,但是在现在互联网和大数据的时代,区块链和我们每一个人都是相关的,俺们区块链技术入门应该学习什么呢? 区块链是分布式数据存储、点对点传输、共识机制、加密算法等计算机技术的新型应用模式。所谓共识机制是区块链系统中实现不同节点之间建立信任、获取权益的数学算法。区块链是比特币的底层技术,像一个数据库账本,记载所有的交易记录。这项技术也因其安全、便捷的特性逐渐得到了银行与金融业的关注。 看下面的例子: 比特币是用C ++语言实现的 以太坊有三种不同的编程语言的参考实现:Go语言实现的Geth客户端,C ++语言实现的Eth客户端以及Python语言实现的PyEthApp。 区块链应用平台Lisk采用JavaScript构建。利用JavaScript强大的API,该平台将允许在纯JavaScript中构建不同的区块链应用程序。 在其他语言中还有更多的区块链实现,如Rust,Ruby和ERLANG。 通过简单了解什么是分布式账本和什么是对等网络这些基本原则,您无法熟练掌握区块链技术。你需要能够实现这些原则。唯一的方法是学习相关的编程语言。要了解区块链技术,请按照以下步骤操作。 找出最相关的实现语言 区块链技术的应用范围很广,从智能合约到记录管理

Is it possible to use the standard library to spawn a process without showing the console window in Windows?

邮差的信 提交于 2020-03-25 19:25:27
问题 This is what I have right now: Command::new("/path/to/application") .args("-param") .spawn() It looks like Rust uses CreateProcessW for running Windows processes, which allows creation flags. Perhaps there is a flag which will do what I need? 回答1: std:os::windows::process::CommandExt extends the process::Command builder with windows-specific options when building for windows. Though no constant is defined for CREATE_NO_WINDOW so you'd either need to define it yourself or use the raw value of

Swapping Elements of a Slice (in-place)

不打扰是莪最后的温柔 提交于 2020-03-25 18:37:07
问题 I have posted my solution in the answers below. The question will not be updated with even more code to not further increase clutter. I'm trying to rotate all elements in a Vec<Vec<T>> clockwise. The vector is guaranteed to be square, as in v.len() == v[0].len() . The idea is to find all elements that are equivalent under rotational symmetry to v 's center swap these elements in place, using std::mem::swap My current code does not change the state of the vec. How do I fix this? fn rotate<T>(v

Shared mutable state in Hyper

ぃ、小莉子 提交于 2020-03-25 04:41:14
问题 I'm trying to create a counter in a Hyper web server that counts the number of requests it has received. I'm using a Arc<Mutex<u64>> to hold onto count. However, I haven't been able to figure out the right combination of move and .clone() to satisfy the types of the closures. Here's some code that compiles, but resets the counter on each request: extern crate hyper; use hyper::rt::Future; use hyper::service::service_fn_ok; use hyper::{Body, Response, Server}; use std::sync::{Arc, Mutex}; fn