rust

How to pattern match on values inside a type implementing Deref, such as Box, without copying the contents?

荒凉一梦 提交于 2020-07-09 07:30:30
问题 I have data contained inside a Box , and would like to pattern match on it without accidentally copying the Box 's contents from the heap to the stack; how do I do that? Let's assume the following code: enum SomeEnum { SomeEntry, AnotherEntry, } fn main() { let boxed_value = Box::new(SomeEnum::AnotherEntry); match *boxed_value { SomeEnum::SomeEntry => {} SomeEnum::AnotherEntry => {} } } Does this copy the enum out of the box onto the stack and pattern match on that copy, or does it do the

How do I execute an async/await function without using any external dependencies?

家住魔仙堡 提交于 2020-07-09 05:26:05
问题 I am attempting to create simplest possible example that can get async fn hello() to eventually print out Hello World! . This should happen without any external dependency like tokio , just plain Rust and std . Bonus points if we can get it done without ever using unsafe . #![feature(async_await)] async fn hello() { println!("Hello, World!"); } fn main() { let task = hello(); // Something beautiful happens here, and `Hello, World!` is printed on screen. } I know async/await is still a nightly

How do I execute an async/await function without using any external dependencies?

穿精又带淫゛_ 提交于 2020-07-09 05:23:30
问题 I am attempting to create simplest possible example that can get async fn hello() to eventually print out Hello World! . This should happen without any external dependency like tokio , just plain Rust and std . Bonus points if we can get it done without ever using unsafe . #![feature(async_await)] async fn hello() { println!("Hello, World!"); } fn main() { let task = hello(); // Something beautiful happens here, and `Hello, World!` is printed on screen. } I know async/await is still a nightly

How do I execute an async/await function without using any external dependencies?

一曲冷凌霜 提交于 2020-07-09 05:23:25
问题 I am attempting to create simplest possible example that can get async fn hello() to eventually print out Hello World! . This should happen without any external dependency like tokio , just plain Rust and std . Bonus points if we can get it done without ever using unsafe . #![feature(async_await)] async fn hello() { println!("Hello, World!"); } fn main() { let task = hello(); // Something beautiful happens here, and `Hello, World!` is printed on screen. } I know async/await is still a nightly

How do I spawn many cancellable timers using Tokio?

隐身守侯 提交于 2020-07-09 03:17:30
问题 How do I use Tokio to implement a fixed number of timers that are regularly reset and canceled across threads? When a timer expires, a callback will be executed. An API similar to that of Go's time.AfterFunc is essentially what I desire: package main import ( "fmt" "time" ) func main() { t := time.AfterFunc(time.Hour, func() { // happens every 2 seconds with 1 second delay fmt.Println("fired") }) for { t.Reset(time.Second) time.Sleep(time.Second * 2) } } The only crate I've found that

How do I share a struct containing a phantom pointer among threads?

一曲冷凌霜 提交于 2020-07-08 12:27:25
问题 I have a structure that needs to be generic over a type, yet the type is not actually contained in the structure: it's used in methods of this structure, not in the structure itself. And so, the structure includes a PhantomData member: pub struct Map<T> { filename: String, phantom: PhantomData<*const T>, } The phantom member is defined as a pointer because the structure does not actually own data of type T . This is per advice in the documentation of std::marker::PhantomData: Adding a field

How to specify the exact version of a dependency?

馋奶兔 提交于 2020-07-08 11:58:05
问题 I'm using $ cargo --version cargo 0.21.0-beta (7e00b82d9 2017-07-17) I created a simple project with cargo new --bin test1 , and then I added a dependency: [dependencies] lazy_static = "0.2.2" to Cargo.toml (according to this such version exists) and #[macro_use] extern crate lazy_static; to src/main.rs When I run cargo build : $ cargo build Compiling lazy_static v0.2.8 Compiling test1 v0.1.0 (file:///tmp/test1) warning: unused `#[macro_use]` import --> src/main.rs:1:1 | 1 | #[macro_use] | ^^

Remove an element while iterationg over it [duplicate]

折月煮酒 提交于 2020-07-08 11:54:03
问题 This question already has answers here : Removing elements from a Vec based on some condition (2 answers) Closed 2 years ago . Is there a way to remove an element while iterationg over it? Here is a code example: for particle in &mut particles { let mut delete = false; // Do stuff... if delete { // Remove element from particles vector <-- THIS } } I think it's not possible with this (dirty) design. What's the common (elegant?) pattern to remove some elements in a vector assuming I need to

Remove an element while iterationg over it [duplicate]

三世轮回 提交于 2020-07-08 11:53:09
问题 This question already has answers here : Removing elements from a Vec based on some condition (2 answers) Closed 2 years ago . Is there a way to remove an element while iterationg over it? Here is a code example: for particle in &mut particles { let mut delete = false; // Do stuff... if delete { // Remove element from particles vector <-- THIS } } I think it's not possible with this (dirty) design. What's the common (elegant?) pattern to remove some elements in a vector assuming I need to

How do I test crates with #![no_std]?

会有一股神秘感。 提交于 2020-07-08 06:25:33
问题 I'm writing a runtime for a programming language implementation in Rust. I'm planning on linking in this runtime with the compiled code I generate, so to keep the binary small I don't want to rely on std . When I try to cargo test my runtime, I get errors saying saying that std::slice::AsSlice can't be found, which I found is because some of the test harness requires std library code. How do I go about testing this code? Is there a way to conditionally include the #![no_std] pragma, i.e.