rust

Is it possible to combine two patterns, one with a match guard, in the same match arm?

雨燕双飞 提交于 2021-02-04 14:42:33
问题 I want to check if a string contains '$' and if there is something after the '$': I tried this code: fn test(s: String) { match s.find('$') { None | (Some(pos) if pos == s.len() - 1) => { expr1(); } _ => { expr2(); } } } But it doesn't compile: error: expected one of `)` or `,`, found `if` Is it impossible to combine None and Some in one match-arm? If so, is there a simple way to not duplicate expr1() except moving it into a separate function? 回答1: It is impossible to have the match-guard

Idiomatic way of returning multiple types in Rust [duplicate]

霸气de小男生 提交于 2021-02-04 08:40:48
问题 This question already has an answer here : Multiple return types from a method (1 answer) Closed 4 months ago . I'm writing a program which parses a number of files. These files can fall into a number of different categories and I don't know which in advance. I makes sense to create a type for each type of data the files can contain, but I'm struggling with getting this datatype out of my parser and into my model/main program. In most other programming languages I would probably define a

How to shuffle a vector except for the first and last elements without using third party libraries?

。_饼干妹妹 提交于 2021-02-04 08:39:05
问题 I have a task to shuffle words but the first and last letter of every word must be unchanged. When I try to use filter() it doesn't work properly. const SEPARATORS: &str = " ,;:!?./%*$=+)@_-('\"&1234567890\r\n"; fn main() { print!("MAIN:{:?}", mix("Evening,morning")); } fn mix(s: &str) -> String { let mut a: Vec<char> = s.chars().collect(); for group in a.split_mut(|num| SEPARATORS.contains(*num)) { if group.len() > 4 { let k = group.first().unwrap().clone(); let c = group[group.len() - 1]

How to conditionally deserialize JSON to two different variants of an enum?

邮差的信 提交于 2021-02-04 08:10:15
问题 Let's say I have JSON data like the following: { "type": "A", "value": [ 1, 2, 3, 4, 5 ] } { "type": "B", "value": [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ] ] } type determines the type of value , which in the first example is Vec<u32> and in the second is Vec<Vec<u32>> . If I represent the above data as follows: enum DataValue { TypeA(Vec<u32>), TypeB(Vec<Vec<u32>>) } struct Data { data_type: String, value: DataValue } How do I implement serde deserialization to properly decode these values? 回答1: You

How to conditionally deserialize JSON to two different variants of an enum?

ぃ、小莉子 提交于 2021-02-04 08:05:04
问题 Let's say I have JSON data like the following: { "type": "A", "value": [ 1, 2, 3, 4, 5 ] } { "type": "B", "value": [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ] ] } type determines the type of value , which in the first example is Vec<u32> and in the second is Vec<Vec<u32>> . If I represent the above data as follows: enum DataValue { TypeA(Vec<u32>), TypeB(Vec<Vec<u32>>) } struct Data { data_type: String, value: DataValue } How do I implement serde deserialization to properly decode these values? 回答1: You

How to conditionally deserialize JSON to two different variants of an enum?

孤人 提交于 2021-02-04 08:04:26
问题 Let's say I have JSON data like the following: { "type": "A", "value": [ 1, 2, 3, 4, 5 ] } { "type": "B", "value": [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ] ] } type determines the type of value , which in the first example is Vec<u32> and in the second is Vec<Vec<u32>> . If I represent the above data as follows: enum DataValue { TypeA(Vec<u32>), TypeB(Vec<Vec<u32>>) } struct Data { data_type: String, value: DataValue } How do I implement serde deserialization to properly decode these values? 回答1: You

How to conditionally deserialize JSON to two different variants of an enum?

痞子三分冷 提交于 2021-02-04 08:04:24
问题 Let's say I have JSON data like the following: { "type": "A", "value": [ 1, 2, 3, 4, 5 ] } { "type": "B", "value": [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ] ] } type determines the type of value , which in the first example is Vec<u32> and in the second is Vec<Vec<u32>> . If I represent the above data as follows: enum DataValue { TypeA(Vec<u32>), TypeB(Vec<Vec<u32>>) } struct Data { data_type: String, value: DataValue } How do I implement serde deserialization to properly decode these values? 回答1: You

Spawning tasks with non-static lifetimes with tokio 0.1.x

纵然是瞬间 提交于 2021-02-04 07:08:50
问题 I have a tokio core whose main task is running a websocket (client). When I receive some messages from the server, I want to execute a new task that will update some data. Below is a minimal failing example: use tokio_core::reactor::{Core, Handle}; use futures::future::Future; use futures::future; struct Client { handle: Handle, data: usize, } impl Client { fn update_data(&mut self) { // spawn a new task that updates the data self.handle.spawn(future::ok(()).and_then(|x| { self.data += 1; //