rust

Change selector in match when selector is a mutable reference

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 04:08:24
问题 I want to change enum variant based on some properties of the current enum variant in Iterator::next . I have two attempts, neither of which compile: enum Test { A(Vec<usize>), B, } impl<'a> Iterator for Test { type Item = usize; fn next(&mut self) -> Option<Self::Item> { // attempt 1 if let Test::A(ref a) = *self { if a.len() == 0 { *self = Test::B; // doesn't work because a is borrowed }; } // attempt 2 *self = match *self { Test::A(ref a) if a.len() == 0 => Test::B, _ => *self, // cannot

Converting an enum where all variants implement the same trait to a box in Rust?

被刻印的时光 ゝ 提交于 2020-01-04 04:05:08
问题 I have a trait Foo , with some implementations, together with an enum Foos with one variant per implemention. I want to be able to convert my enum into Box<dyn Foo> . This is my current solution: trait Foo {} struct FooA {} impl Foo for FooA {} struct FooB {} impl Foo for FooB {} struct FooC {} impl Foo for FooC {} enum Foos { A(FooA), B(FooB), C(FooC), } impl Foos { fn into_box(self) -> Box<dyn Foo> { match self { Foos::A(foo) => Box::new(foo), Foos::B(foo) => Box::new(foo), Foos::C(foo) =>

How do I map a C struct with padding over 32 bytes using serde and bincode?

那年仲夏 提交于 2020-01-04 03:18:26
问题 I'm mapping a binary structure using serde and bincode. #[macro_use] extern crate serde_derive; extern crate serde; extern crate bincode; #[derive(Serialize, Deserialize)] struct Superblock { magic: [u8; 16], //reserved: [u8; 492], crc: u32, } Things work as expected, but I can't map the reserved field. Apparently fixed size arrays are only defined for sizes up to 32 bytes. How do I register my custom-sized array so that the padding gets deserialised? Is serde+bincode the right approach? I

Does Cargo support custom profiles?

自古美人都是妖i 提交于 2020-01-04 03:00:59
问题 I often want to compile in release mode with debug = true so that I can read the generated assembly a bit easier. I am currently doing this: [profile.release] debug = true but I don't want any debug symbols in my final release build. I'd like to do something like: [profile.custom] debug = true opt-level = 3 rpath = false lto = true debug-assertions = false codegen-units = 1 panic = 'unwind' And then run cargo build --custom I've read the documentation to no avail. 回答1: Does Cargo support

Does Cargo support custom profiles?

天大地大妈咪最大 提交于 2020-01-04 02:59:46
问题 I often want to compile in release mode with debug = true so that I can read the generated assembly a bit easier. I am currently doing this: [profile.release] debug = true but I don't want any debug symbols in my final release build. I'd like to do something like: [profile.custom] debug = true opt-level = 3 rpath = false lto = true debug-assertions = false codegen-units = 1 panic = 'unwind' And then run cargo build --custom I've read the documentation to no avail. 回答1: Does Cargo support

Weird behaviour when using read_line in a loop

瘦欲@ 提交于 2020-01-04 01:58:19
问题 My first program in Rust is supposed to take input from the user in the form of characters, either C or F: use std::io; fn main() { let mut srcunit = String::new(); let mut switch = true; while switch { println!("source unit? F or C?"); io::stdin().read_line(&mut srcunit).expect( "failed to read src unit", ); if srcunit.trim() == "F" || srcunit.trim() == "C" { println!("doing things right with {}", srcunit); switch = false; } else { println!("either F or C, not {}", srcunit); } } println!(

collect values of a hashmap into a vector

和自甴很熟 提交于 2020-01-04 01:56:07
问题 I am new to Rust and can not find the way of collecting the values of a hashmap into a vector in the documentation. Let's say I have a hashmap: score_table: HashMap<Id, Score> and I want to get all the Score into a Vec all_scores: Vec<Score> I was tempted to use the values but it does not work since values is not a vec: all_scores = score_table.values() I know that Values implement the ExactSizeIterator trait, but I do not know how to collect all values of an iterator into a vector without

How to get tokio-io's async_read for a File handle

大城市里の小女人 提交于 2020-01-04 01:53:09
问题 I want to stream lines out of a file handle and I do not know how to satisfy the trait bound that a File has async_read : use std::fs::File; use std::io::{ BufReader, BufRead }; use tokio_core::reactor::Handle; use tokio_io::io::lines; use tokio_io::AsyncRead; struct DockerLog { path: String } impl DockerLog { pub fn new(path: String) -> DockerLog { DockerLog { path: path } } pub fn read_lines(&self, handle: &Handle) { let file : File = File::open(&self.path[..]).unwrap(); let l = lines

Why does Rust have both call by value and call by reference?

ぐ巨炮叔叔 提交于 2020-01-04 01:49:13
问题 Some languages, like Haskell, make no distinction between pass-by-value and pass-by-reference. The compiler can then approximately choose the most efficient calling convention with a heuristic. One example heuristic would be for the Linux x64 ABI: if the size of parameter is greater than 16 bytes, pass a pointer to the stack otherwise pass the value in registers. What is the advantage of keeping both notions of pass-by-value and pass-by-reference (non-mutable of course) in Rust and forcing

How can the compilation properties be determined in a procedural macro?

瘦欲@ 提交于 2020-01-03 18:56:15
问题 I'm working on a procedural macro that does a lot of work that can slow down compilation considerably. The work done does not effect the semantics of the function; that is, if given the same set of arguments, the returned value is does not change depending on whether the macro is applied. In an effort to make the edit-comp-test loop quicker, I would like to make the macro a no-op depending on conditions that relate to how the crate is being compiled. I would like to be able to determine two