rust

Passing two objects, where one holds a reference to another, into a thread

自闭症网瘾萝莉.ら 提交于 2021-02-20 11:51:10
问题 I have two objects where the second one requires the fist one to outlive it because it holds a reference to the first one. I need to move both of them into a thread, but the compiler is complaining that the first one doesn't live long enough. Here is the code: use std::thread; trait Facade: Sync { fn add(&self) -> u32; } struct RoutingNode<'a> { facade: &'a (Facade + 'a), } impl<'a> RoutingNode<'a> { fn new(facade: &'a Facade) -> RoutingNode<'a> { RoutingNode { facade: facade } } } fn main()

How to move a Vec<Box<dyn Trait>> Into Vec<Rc<RefCell<dyn Trait>>>

浪子不回头ぞ 提交于 2021-02-20 10:39:12
问题 I have a Vec<Box<dyn Trait>> as input, and I want to store its elements in a Vec<Rc<RefCell<dyn Trait>>> . What is the best way to do it? I tried with: use std::cell::RefCell; use std::rc::Rc; trait Trait {} fn main() { let mut source: Vec<Box<dyn Trait>> = Vec::new(); let mut dest: Vec<Rc<RefCell<dyn Trait>>> = Vec::new(); for s in source { let d = Rc::new(RefCell::new(s.as_ref())); dest.push(d); } } But I got the error: error[E0277]: the trait bound `&dyn Trait: Trait` is not satisfied -->

How can I conditionally execute a module-level doctest based on a feature flag?

試著忘記壹切 提交于 2021-02-20 06:40:26
问题 I am writing documentation for a module that has some options controlled by a Cargo feature flag. I'd like to always show this documentation so that consumers of the crate know that it's available, but I need to only run the example when the feature is enabled. lib.rs //! This crate has common utility functions //! //! ``` //! assert_eq!(2, featureful::add_one(1)); //! ``` //! //! You may also want to use the feature flag `solve_halting_problem`: //! //! ``` //! assert!(featureful::is_p_equal

Why does `&value.into_something()` still result in a moved value?

人盡茶涼 提交于 2021-02-20 02:54:27
问题 I'm struggling to see how this transfers ownership. Here is my code: let res = screenshot::take_screenshot(0); let file = File::open("test.png").expect("Failed to open file"); let encoder = PNGEncoder::new(file); encoder.encode(&res.into_raw(), res.width(), res.height(), ColorType::RGBA(0) ); screenshot::take_screenshot is a function that returns an ImageBuffer<Rgba<u8>, Vec<u8>> . Here is the compiler error I'm getting: error[E0382]: use of moved value: `res` --> src/main.rs:21:37 | 21 |

rust compile x86 library on x86_64 machine

穿精又带淫゛_ 提交于 2021-02-19 09:45:29
问题 I have ubuntu x86_64 container and cargo build goes well. But i need to build x86 library version too. As far as I understand i need to add i686 toolchain and target. rustup target add i686-unknown-linux-gnu done successful rustup toolchain install stable-i686-unknown-linux-gnu finished with error $ rustup toolchain install stable-i686-unknown-linux-gnu info: syncing channel updates for 'stable-i686-unknown-linux-gnu' info: latest update on 2018-11-08, rust version 1.30.1 (1433507eb 2018-11

How do I create a HashMap with type erased keys?

假如想象 提交于 2021-02-19 07:48:05
问题 I want to be able to use a variety of different types as keys in a HashMap , all of which would implement Hash . This seems like it should be possible: from reading the docs it seems that every Hasher will produce a u64 result, so they eventually get reduced down to a common type. Effectively I want to do: use std::{collections::HashMap, hash::Hash}; fn x(_: HashMap<Box<dyn Hash>, ()>) {} which I'm not allowed to do: error[E0038]: the trait `std::hash::Hash` cannot be made into an object -->

How to print both the index and value for every element in a Vec?

£可爱£侵袭症+ 提交于 2021-02-19 06:42:34
问题 I'm trying to complete the activity at the bottom of this page, where I need to print the index of each element as well as the value. I'm starting from the code use std::fmt; // Import the `fmt` module. // Define a structure named `List` containing a `Vec`. struct List(Vec<i32>); impl fmt::Display for List { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Extract the value using tuple indexing // and create a reference to `vec`. let vec = &self.0; write!(f, "[")?; // Iterate over

How to print both the index and value for every element in a Vec?

拜拜、爱过 提交于 2021-02-19 06:42:11
问题 I'm trying to complete the activity at the bottom of this page, where I need to print the index of each element as well as the value. I'm starting from the code use std::fmt; // Import the `fmt` module. // Define a structure named `List` containing a `Vec`. struct List(Vec<i32>); impl fmt::Display for List { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Extract the value using tuple indexing // and create a reference to `vec`. let vec = &self.0; write!(f, "[")?; // Iterate over

Is it possible to use a single generic for both key and value of a HashMap?

风格不统一 提交于 2021-02-19 06:08:05
问题 In chapter 13 of the Rust book, you implement a Cacher to use memoization to demonstrate functional programming and how to speed up long-running tasks. As an extra challenge, they recommend making the Cacher allow multiple keys using a HashMap and also leveraging generics to allow more flexibility. Try modifying Cacher to hold a hash map rather than a single value. The keys of the hash map will be the arg values that are passed in, and the values of the hash map will be the result of calling

Is it possible to use a single generic for both key and value of a HashMap?

不问归期 提交于 2021-02-19 06:06:10
问题 In chapter 13 of the Rust book, you implement a Cacher to use memoization to demonstrate functional programming and how to speed up long-running tasks. As an extra challenge, they recommend making the Cacher allow multiple keys using a HashMap and also leveraging generics to allow more flexibility. Try modifying Cacher to hold a hash map rather than a single value. The keys of the hash map will be the arg values that are passed in, and the values of the hash map will be the result of calling