rust

Destructure a vector into variables and give away ownership?

和自甴很熟 提交于 2020-01-04 06:05:09
问题 I have a struct struct Foo { foo1: String, foo2: String, foo3: String, foo4: String, // ... } I would like to create an instance of Foo from a vector. let x = vec!["a".to_string(), "b".to_string(), "c".to_string(), "d".to_string()]; match x.as_slice() { &[ref a, ref b, ref c, ref d] => { let foo = Foo { foo1: a.to_string(), foo2: b.to_string(), foo3: c.to_string(), foo4: d.to_string(), }; }, _ => unreachable!(), } Do I have to copy the strings? Is there any better way to destructure the

Destructure a vector into variables and give away ownership?

不想你离开。 提交于 2020-01-04 06:05:05
问题 I have a struct struct Foo { foo1: String, foo2: String, foo3: String, foo4: String, // ... } I would like to create an instance of Foo from a vector. let x = vec!["a".to_string(), "b".to_string(), "c".to_string(), "d".to_string()]; match x.as_slice() { &[ref a, ref b, ref c, ref d] => { let foo = Foo { foo1: a.to_string(), foo2: b.to_string(), foo3: c.to_string(), foo4: d.to_string(), }; }, _ => unreachable!(), } Do I have to copy the strings? Is there any better way to destructure the

Expected std::iter::Iterator, but std::iter::Iterator found

旧街凉风 提交于 2020-01-04 05:54:10
问题 I am trying to express the following: Given a matrix and two index increments, return all quadruplets of numbers from the matrix: quadruplets of numbers along the rows, or along the columns, or along some diagonal. use std::iter::Iterator; use std::iter::Peekable; use std::ops::Range; struct Quads<'a> { mx: &'a Vec<Vec<u32>>, xs: &'a mut Peekable<Range<i32>>, ys: &'a mut Peekable<Range<i32>>, dx: i32, dy: i32, } impl<'a> Quads<'a> { fn new(mx: &'a Vec<Vec<u32>>, dx: i32, dy: i32) -> Quads<'a>

Expected std::iter::Iterator, but std::iter::Iterator found

独自空忆成欢 提交于 2020-01-04 05:52:35
问题 I am trying to express the following: Given a matrix and two index increments, return all quadruplets of numbers from the matrix: quadruplets of numbers along the rows, or along the columns, or along some diagonal. use std::iter::Iterator; use std::iter::Peekable; use std::ops::Range; struct Quads<'a> { mx: &'a Vec<Vec<u32>>, xs: &'a mut Peekable<Range<i32>>, ys: &'a mut Peekable<Range<i32>>, dx: i32, dy: i32, } impl<'a> Quads<'a> { fn new(mx: &'a Vec<Vec<u32>>, dx: i32, dy: i32) -> Quads<'a>

Can't access fields of structs in implementations of dynamic traits [duplicate]

筅森魡賤 提交于 2020-01-04 05:37:36
问题 This question already has answers here : Expected type parameter, found u8, but the type parameter is u8 (2 answers) Closed 3 years ago . While trying to implement traits with generic arguments and access the fields of those generic arguments, I've encountered an error message saying that the arguments in question do not contain such fields. Here's some example code that exhibits the issue: pub struct Settings { pub time: String, } pub trait Foo { fn get<T>(t: T); } struct Bar; impl Foo for

How to access current cargo profile (debug/release, …) from the build script (build.rs)

五迷三道 提交于 2020-01-04 05:21:13
问题 In an embedded project, I usually run the debug mode with qemu, but need to build the release for a concrete microcontroller. The build.rs would need to know what the actual mode is (debug or release) to generate the correct memory layout. How can the build.rs make this decision? Related: How to access current cargo profile (build, test, bench, doc, ....) from the build script (build.rs) 回答1: It's written in the doc: PROFILE - "release" for release builds, "debug" for other builds. fn main()

Why is what timers show so counter-intuitive?

≡放荡痞女 提交于 2020-01-04 05:06:29
问题 I am making a parser of some text. I need to support unicode texts, that's why I am using the String::chars iterator: playground use std::time::Instant; fn main() { let text = "a".repeat(10000); let mut timer1 = 0; let mut timer2 = 0; let start1 = Instant::now(); for pos in 1..10000 { let start2 = Instant::now(); let ch = text.chars().nth(pos).unwrap(); timer2 += start2.elapsed().as_millis(); } timer1 += start1.elapsed().as_millis(); println!("timer1: {} timer2: {}", timer1, timer2); }

How to match on data type in Rust?

两盒软妹~` 提交于 2020-01-04 04:46:06
问题 I'm trying to match on the datatype of a generic field of a struct and react accordingly. My general idea was like this (code doesn't compile): struct Foo<T> { bar: T, } fn main() { let x = Foo::<String> { bar: "world".to_string(), }; match x.bar { String => println!("It's a string!"), u32 => println!("It's a u32!"), _ => println!("Something else"), }; println!("end of program!"); } The error message from the compiler: warning: unreachable pattern --> src/main.rs:12:9 | 11 | String => println

How to construct const integers from literal byte expressions?

淺唱寂寞╮ 提交于 2020-01-04 04:30:28
问题 Is there a way to construct a const integer from a literal byte expression, either using a byte string or a macro which constructs the integer? For example: const MY_ID: u16 = u16_code!(ID); const MY_WORD: u32 = u32_code!(WORD); const MY_LONG: u64 = u64_code!(LONGWORD); Or something similar, passing in b"ID" instead of ID ? * It should fail to compile when the wrong number of characters are passed too, something I couldn't figure out how to achieve when using bit-shifting on a literal byte

How can I get a list of types that implement a particular trait in Rust?

你说的曾经没有我的故事 提交于 2020-01-04 04:09:08
问题 I want to know a struct that implements std::io::Write ; is it described in some document? 回答1: When you lookup the API for std you can search for your trait there (e.g. std::io::Write). When you scroll down to the section "Implementors" you will see all structs/enums that implement that trait in std . To get a better overview, you can use the + or - keys to collapse all sections and have a nice overview, e.g. 来源: https://stackoverflow.com/questions/55216559/how-can-i-get-a-list-of-types-that