rust

How do I copy/clone a struct that derives neither?

耗尽温柔 提交于 2021-01-04 07:07:17
问题 I'm trying to do some game programming with Piston, but i'm struggling with opengl_graphics::Texture , since it does not derive Copy or Clone . extern crate piston_window; extern crate piston; extern crate graphics; extern crate opengl_graphics; use opengl_graphics::Texture as Tex; use piston_window::*; use std::path::Path; use opengl_graphics::GlGraphics; #[derive(PartialEq)] enum ObjectType { Blocking, Passing, } struct Object { sprite: Tex, obj_type: ObjectType, position: Position, }

When do I need to make a closure variable mutable? [duplicate]

六月ゝ 毕业季﹏ 提交于 2021-01-04 07:02:14
问题 This question already has an answer here : When should I add mut to closures? (1 answer) Closed last month . Why do I need to make this closure variable mutable? The closure isn't returning anything, therefore nothing is being stored in the closure variable. This closure is only capturing a value from the environment and incrementing it. fn main() { let mut x = 1; let mut y = || x = x + 1; y(); println!("{}", x); } 回答1: Essentially, this is a consequence of one of Rust's soundness rules – you

Size of a box containing a struct with a trait parameter

半世苍凉 提交于 2021-01-04 06:53:34
问题 I need a struct that contains a trait object and more of itself. Disappointedly the following code does not compile: trait Foo {} struct Bar<T: Foo> { bars: Vec<Box<Bar<dyn Foo>>>, foo: T, } I managed to coerce this into compiling by adding the ?Sized bound to T , but I do not understand why this should be the case. I assume this is because all trait objects have the same size, but the size of Bar depends on the size of the concrete type T . If so, how is Bar with an unsized T represented in

Does cloning an iterator copy the entire underlying vector?

情到浓时终转凉″ 提交于 2021-01-04 03:14:10
问题 I would like to iterate over a vector several times: let my_vector = vec![1, 2, 3, 4, 5]; let mut out_vector = vec![]; for i in my_vector { for j in my_vector { out_vector.push(i * j + i + j); } } The j-loop has a "value used here after move" error. I know that I can place an & before the two my_vector s and borrow the vectors, but it is nice to have more than one way to do things. I would like a little insight as well. Alternatively, I can write the following: let i_vec = vec![1, 2, 3, 4, 5,

Does cloning an iterator copy the entire underlying vector?

有些话、适合烂在心里 提交于 2021-01-04 03:12:47
问题 I would like to iterate over a vector several times: let my_vector = vec![1, 2, 3, 4, 5]; let mut out_vector = vec![]; for i in my_vector { for j in my_vector { out_vector.push(i * j + i + j); } } The j-loop has a "value used here after move" error. I know that I can place an & before the two my_vector s and borrow the vectors, but it is nice to have more than one way to do things. I would like a little insight as well. Alternatively, I can write the following: let i_vec = vec![1, 2, 3, 4, 5,

Rust编程进阶:013、结构体中的生命周期

落爺英雄遲暮 提交于 2021-01-04 01:00:34
#[derive(Debug)] struct A<'a> { name: &'a str, } fn main() { let n = String::from("hello"); let a = A { name: &n }; println!("a = {:#?}", a); } 本节全部源代码: https://github.com/anonymousGiga/learn_rust/blob/master/learn_life3/src/main.rs 来源: oschina 链接: https://my.oschina.net/u/943779/blog/4874637

Rust recursive macro not working for generating struct

余生颓废 提交于 2021-01-03 08:47:05
问题 I am trying to write a macro that generates a struct in Rust. This macro will add different Serde attributes to struct fields based on the type of field. This is the final goal. For now, I'm simply trying to write a macro that uses another macro for generating a recursive code. This is what the code looks like: macro_rules! f_list { ($fname: ident, $ftype: ty) => { pub $fname: $ftype, } } macro_rules! mk_str { ($sname: ident; $($fname: ident: $ftype: ty,)+) => { #[derive(Debug, Clone)] pub

How to remotely shut down running tasks with Tokio

回眸只為那壹抹淺笑 提交于 2021-01-03 08:29:46
问题 I have a UDP socket that is receiving data pub async fn start() -> Result<(), std::io::Error> { loop { let mut data = vec![0; 1024]; socket.recv_from(&mut data).await?; } } This code is currently blocked on the .await when there is no data coming in. I want to gracefully shut down my server from my main thread, so how do I send a signal to this .await that it should stop sleeping and shut down instead? 回答1: Note: This answer currently links to the 1.x version of Tokio, but applies to Tokio 0

How to remotely shut down running tasks with Tokio

 ̄綄美尐妖づ 提交于 2021-01-03 08:28:51
问题 I have a UDP socket that is receiving data pub async fn start() -> Result<(), std::io::Error> { loop { let mut data = vec![0; 1024]; socket.recv_from(&mut data).await?; } } This code is currently blocked on the .await when there is no data coming in. I want to gracefully shut down my server from my main thread, so how do I send a signal to this .await that it should stop sleeping and shut down instead? 回答1: Note: This answer currently links to the 1.x version of Tokio, but applies to Tokio 0

How can I run a set of functions on a recurring interval without running the same function at the same time using only the standard Rust library?

瘦欲@ 提交于 2021-01-03 07:34:25
问题 I would like to use Rust to create a simple scheduler in order to run multiple concurrent functions at a defined time but do not start more if they haven't finished. For example, if the defined interval is one second, the scheduler should run the functions and don't start more if the previous functions have not returned. The goal is to prevent running the same function multiple times. I created a working example with Go like this: package main import ( "fmt" "sync" "time" ) func myFunc(wg