rust

Is it possible to make a recursive closure in Rust?

巧了我就是萌 提交于 2020-01-26 17:10:51
问题 This is a very simple example, but how would I do something similar to: let fact = |x: u32| { match x { 0 => 1, _ => x * fact(x - 1), } }; I know that this specific example can be easily done with iteration, but I'm wondering if it's possible to make a recursive function in Rust for more complicated things (such as traversing trees) or if I'm required to use my own stack instead. 回答1: There are a few ways to do this. You can put closures into a struct and pass this struct to the closure. You

Is it possible to make a recursive closure in Rust?

别等时光非礼了梦想. 提交于 2020-01-26 17:08:54
问题 This is a very simple example, but how would I do something similar to: let fact = |x: u32| { match x { 0 => 1, _ => x * fact(x - 1), } }; I know that this specific example can be easily done with iteration, but I'm wondering if it's possible to make a recursive function in Rust for more complicated things (such as traversing trees) or if I'm required to use my own stack instead. 回答1: There are a few ways to do this. You can put closures into a struct and pass this struct to the closure. You

Does actix_web's App::register_data create a single instance or one instance per thread?

余生颓废 提交于 2020-01-26 04:44:31
问题 I am trying to set up a global state for my actix_web::HttpServer , and it seems like register_data is the proper API (I could be wrong). From the documentation, it is not clear to me how to create a single instance of application data shared by all HttpServer threads. Here is my code piece: HttpServer::new(|| { App::new() .register_data(web::Data::new(Mutex::new(MyServer::new()))) .service(web::resource("/myservice").route(web::post().to(my_service))) .service(web::resource("/list").to(list

How can you resolve the need to mutably borrow in different match arms in Rust?

本小妞迷上赌 提交于 2020-01-25 20:51:46
问题 When compiling this part of a Piston game, I need to mutably borrow an object in different match arms. As far as I can tell, they should all go out of scope of each other, but yet a compile error is generated. A simplified example that demonstrates the issue: extern crate opengl_graphics; extern crate piston_window; use opengl_graphics::GlGraphics; use opengl_graphics::glyph_cache::GlyphCache; use piston_window::{Button, Context, Input, Key, OpenGL, PistonWindow, Size, text, WindowSettings};

Rust language

戏子无情 提交于 2020-01-25 09:16:58
今天看到了一门新语言Rust 简单看了一下,是由Mozilla开发的。最近出现的语言太多,前段时间刚看Vala,以为Rust是同样的语言。 当看到 http://doc.rust-lang.org/doc/tutorial.html 满是函数式的风格时,我惊奇了。 然后我又不自主地看其面向对象的实现。类似于Go的优雅。 http://www.osnews.com/story/25535/The_Rust_Compiler_0_1_Released 有讨论,真正的变数还是在操作系统与语言的结合。 想到如今该是变革的时候了。Simula系的C++, Java, C#是被强推上历史舞台,但这并不是它们的舞台。 衷心祝愿Go, Rust充分发展,大展身手。 来源: https://www.cnblogs.com/Chrome/archive/2012/01/25/2329403.html

在Rust中使用C语言的库功能

一曲冷凌霜 提交于 2020-01-25 09:16:05
主要是了解unsafe{}语法块的作用。 #[repr(C)] #[derive(Copy, Clone)] #[derive(Debug)] struct Complex { re: f32, im: f32, } #[link(name = "m")] extern { fn ctanf(z: Complex) -> Complex; } fn tan(z: Complex) -> Complex { unsafe { ctanf(z) } } fn main() { let z = Complex { re: -1., im: 1. }; // z is -1 + i let z_tan = tan(z); println!("the tangens of {:?} is {:?}", z, z_tan); } 来源: https://www.cnblogs.com/aguncn/p/11481272.html

A First Look at Rust Language

独自空忆成欢 提交于 2020-01-25 09:15:45
文 Akisann@CNblogs / zhaihj@Github 本篇文章同时发布在Github上:http://zhaihj.github.io/a-first-look-at-rust.html 过去的一年半多,我一直沉迷与 OOC ,原因倒是很简单,OOC是目前为止我所能见到的最容易理解和最容易书写的语言。并且另外一个极其重要的地方是,它可以编译成C代码。 编译成C代码 ,也就意味着优化可以交给高度发展的C语言编译器来做,听起来似乎适合十分高效的方法。 最近几年类似的语言越来越多,从很久很久之前就存在却一直没出名的 Haxe ,还有最近的 Nim-lang ,以及采用了类似ruby语法的 Crystal ,甚至包括编译成C++的 felix 。这些语言都号称自己考虑了速度(运行速度),至少从编译成C/C++的层面上。 可惜的是,在改进OOC编译器 rock 的过程中,我遇到了越来越多的问题,这些问题让喜欢速度的人泄气。一个最明显的事情是,这些语言几乎都用了GC,不论是libGC还是自己写的,并且更重要的是,很多语言特性是基于GC设计的——比如闭包,比如iterator的unwrap,在有没GC的情况下,这些东西的设计要复杂的多。在OOC里,由于Generics不是Template,更多的东西开始依存GC,在用了它一年后,当我真正开始在工作里使用的时候,这些问题开始出现

Rust 泛型

杀马特。学长 韩版系。学妹 提交于 2020-01-25 09:15:35
泛型可以使用在结构体中 struct Pair<T> { x: T, y: T, } 其中x,y都属于T类型。 实现结构体的方法或者关联函数需要在 impl 关键字后面指定泛型 impl<T> Pair<T> { fn new(x: T, y: T) -> Self { Self { x, y, } } } impl<T> Point<T> { fn x(&self) -> &T { &self.x } } 讲到泛型就绕不开 trait ,trait类似于其他语言中的接口 具体使用方法如下 pub trait Summarizable { fn summary(&self) -> String; } pub struct NewsArticle { pub headline: String, pub location: String, pub author: String, pub content: String, } impl Summarizable for NewsArticle { fn summary(&self) -> String { format!("{}, by {} ({})", self.headline, self.author, self.location) } } 要希望泛型拥有特定的功能,就必须指定泛型的trait,简称trait bound impl

Is there a way that we can convert from futures 0.1 to the standard library futures?

人走茶凉 提交于 2020-01-25 08:57:45
问题 The async / await feature is coming soon, but there are a lot of libraries still using futures 0.1. How do we convert between the two? Convert an async future to 0.1 future covers converting an async future to 0.1 future. How do I erase the type of future in the new future API? talks about an async function that calls an 0.1 future and gets the result, but where is the await!() macro I can import? It seems it no longer compiles. struct A_future01; impl A_future01 { pub fn exec1() -> Box<dyn

Unable to tokio::run a boxed Future because the trait bound Send is not satisfied [duplicate]

杀马特。学长 韩版系。学妹 提交于 2020-01-25 08:27:45
问题 This question already has answers here : The trait bound `futures::Future<Item=Arc<T>, Error=Box<Error + Send>>: Send` is not satisfied (1 answer) Sending trait objects between threads in Rust (1 answer) Closed 2 years ago . I have a function that should optionally run a future or do nothing, depending on parameters. I tried putting a Box around the two futures that will be returned, a tokio::prelude::future::Done<Item=(), Error=()> that immediately resolves to Ok(()) , and a tokio::timer: