rust

error[E0597]: borrowed value does not live long enough in While loop

▼魔方 西西 提交于 2020-05-24 03:30:05
问题 I am really new to Rust, I am having trouble solving this error, but it only happens if I comment out the while statement , basicly I am asking values from the console and storing it in a HashMap: use std::collections::HashMap; use std::io; fn main() { let mut customers = HashMap::new(); let mut next_customer = true; while next_customer { let mut input_string = String::new(); let mut temp_vec = Vec::with_capacity(3); let mut vec = Vec::with_capacity(2); println!("Insert new customer f.e =

How should I reduce repetition in rust type signatures?

霸气de小男生 提交于 2020-05-23 09:42:46
问题 I have the following working code that isn't very DRY: impl<'a, G, E, N, EW, NW, ER, NOW, EOW> Overlay<'a, G, E, N, EW, NW, ER, NOW, EOW> where &'a G: GraphBase<EdgeId = E, NodeId = N> + Data<EdgeWeight = EW, NodeWeight = NW> + DataMap, ER: EdgeRef<Weight = EW, EdgeId = E, NodeId = N>, E: Copy + Eq + Hash, N: Copy + Eq + Hash, { fn overlayed_elements(&'a self) -> OverlayedItems<'a, G, E, N, EW, NW, ER, NOW, EOW>{ OverlayedItems{ overlay: self, phase: Phase::Nodes(self.nodes.iter()), node

Is it possible to print a backtrace in Rust without panicking?

情到浓时终转凉″ 提交于 2020-05-23 08:38:48
问题 Is it possible to print a backtrace (assuming RUST_BACKTRACE is enabled) without panicking? It seems that the only way of doing that is calling via panic! . If not, is there a reason for it? 回答1: Rust uses the backtrace crate to print the backtrace in case of panics (has been merged in PR #60852). A simple example can be found in the crate documentation use backtrace::Backtrace; fn main() { let bt = Backtrace::new(); // do_some_work(); println!("{:?}", bt); } which gives for example stack

Why does a doubly-reversed iterator act as if it was never reversed?

我们两清 提交于 2020-05-23 08:15:47
问题 I have an input vector which contains numbers. In an output vector, I need to get a sequence of partial products but in right-to-left order. The last element of the output must be equal to the last one in the input; the second-to-last element of the output must be a product of the last and second-to-last elements of input; and so on. For example, if the input vector is let input = vec![2, 3, 4]; then I need the output to be [24, 12, 4] . My implementation takes an iterator over the input,

Why does “can't leak private type” only apply to structs and not enums?

╄→гoц情女王★ 提交于 2020-05-22 08:03:11
问题 In Learning Rust With Entirely Too Many Linked Lists, they show that a pub enum can't hold a private struct :, struct Node { elem: i32, next: List, } pub enum List { Empty, More(Box<Node>), } This will cause the compiler to complain: error[E0446]: private type `Node` in public interface --> src/main.rs:8:10 | 8 | More(Box<Node>), | ^^^^^^^^^^ can't leak private type But this code will not cause an error even though Link is private: pub struct List { head: Link, } enum Link { Empty, More(Box

Dependending on what variant an enum is, how to return one of 2 different possible types inside it?

风格不统一 提交于 2020-05-17 08:49:24
问题 Similar questions have been asked many times before, but the closest I have come to my case is here. I'm trying to achieve something like this(although the match part is completely wrong). struct A { a: i8, b: u8, } struct B { c: i16, d: u16, } enum select { type_a(A), type_b(B), } impl select { fn return_one<T>(&self) -> T { match self { type_a(a) => a, type_b(b) => b, } } } As you can see, it has an extra caveat, where the values inside the enum can be either of two structs. Is there some

What is the paradigmatic way to use large global static tables that are initialized once and serve practically as constants in Rust? [duplicate]

孤人 提交于 2020-05-17 07:47:06
问题 This question already has answers here : How do I create a global, mutable singleton? (2 answers) How can you make a safe static singleton in Rust? (2 answers) Closed 4 days ago . I'm trying to port a Go language chess engine ( https://github.com/easychessanimations/gobbit ) to Rust ( https://github.com/easychessanimations/rustengine ). Problem with Go is that it produces a WASM executable which is bloated in size because it contains the whole Go runtime ( > 2 MB ). Also the native executable

What is the paradigmatic way to use large global static tables that are initialized once and serve practically as constants in Rust? [duplicate]

蹲街弑〆低调 提交于 2020-05-17 07:47:06
问题 This question already has answers here : How do I create a global, mutable singleton? (2 answers) How can you make a safe static singleton in Rust? (2 answers) Closed 4 days ago . I'm trying to port a Go language chess engine ( https://github.com/easychessanimations/gobbit ) to Rust ( https://github.com/easychessanimations/rustengine ). Problem with Go is that it produces a WASM executable which is bloated in size because it contains the whole Go runtime ( > 2 MB ). Also the native executable

Rust wasm: How to access Webassembly.Memory of current instance (from Rust)?

五迷三道 提交于 2020-05-17 06:21:46
问题 Looking at the function signature of js_sys::Uint8Array::new_with_byte_offset_and_length pub fn new_with_byte_offset_and_length( buffer: &JsValue, byte_offset: u32, length: u32 ) -> Uint8Array It needs an argument buffer which refers to the current wasm instance's memory buffer. How do I access such an object from the Rust side ? (that gets compiled to wasm) 回答1: It needs an argument buffer which refers to the current wasm instance's memory buffer. First of all, it's worth noting that this is

Method `mul` has an incompatible type for trait

亡梦爱人 提交于 2020-05-17 05:13:03
问题 I'm creating a simple matrix struct in Rust and I'm trying to implement some basic operator methods: use std::ops::Mul; struct Matrix { cols: i32, rows: i32, data: Vec<f32>, } impl Matrix { fn new(cols: i32, rows: i32, data: Vec<f32>) -> Matrix { Matrix { cols: cols, rows: rows, data: data, } } } impl Mul<f32> for Matrix { type Output = Matrix; fn mul(&self, m: f32) -> Matrix { let mut new_data = Vec::with_capacity(self.cols * self.rows); for i in 0..self.cols * self.rows { new_data[i] = self