rust

Does Rust 2018 support “if let” chaining?

大憨熊 提交于 2021-02-08 12:47:21
问题 I'm parsing a vector of tokens, each of enum type. This means I get a lot of code like: if v.len() >= 3 { if let Token::Type1(value1) = &v[0] { if let Token::Type2(value2) = &v[1] { if let Token::Type(value3) = &v[2] { return Parsed123(value1, value2, value3); } } } } This is pretty ugly - and I've worked out that I can do this to make it a little nicer: if v.len() >= 3 { if let (Token::Type1(value1), Token::Type2(value2), Token::Type3(value3)) = (&v[0], &v[1], &v[2]) { return Parsed123

Does Rust 2018 support “if let” chaining?

瘦欲@ 提交于 2021-02-08 12:47:06
问题 I'm parsing a vector of tokens, each of enum type. This means I get a lot of code like: if v.len() >= 3 { if let Token::Type1(value1) = &v[0] { if let Token::Type2(value2) = &v[1] { if let Token::Type(value3) = &v[2] { return Parsed123(value1, value2, value3); } } } } This is pretty ugly - and I've worked out that I can do this to make it a little nicer: if v.len() >= 3 { if let (Token::Type1(value1), Token::Type2(value2), Token::Type3(value3)) = (&v[0], &v[1], &v[2]) { return Parsed123

can i use rust's match in python? [closed]

本秂侑毒 提交于 2021-02-08 12:13:46
问题 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 1 year ago . Improve this question i want to use rust match in python3, instead of if...elif statement. https://doc.rust-lang.org/rust-by-example/flow_control/match.html fn main() { let number = 13; // TODO ^ Try different values for `number` println!("Tell me about {}", number); match number { //

Why is Vec<&str> missing lifetime specifier here? [duplicate]

笑着哭i 提交于 2021-02-08 12:03:32
问题 This question already has an answer here : What does “missing lifetime specifier” mean when storing a &str in a structure? (1 answer) Closed 1 year ago . This code compiles: struct IntDisplayable(Vec<u8>); impl fmt::Display for IntDisplayable { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { for v in &self.0 { write!(f, "\n{}", v)?; } Ok(()) } } fn main() { let vec: Vec<u8> = vec![1,2,3,4,5]; let vec_Foo = IntDisplayable(vec); println!("{}",vec_Foo); } whilst this code doesn't: struct

Rust futures — adapting a function as a Sink

那年仲夏 提交于 2021-02-08 11:15:17
问题 I have something similar to the tokio connect example with a method that accepts a sink: pub async fn connect( addr: &SocketAddr, mut stdin: impl Stream<Item = Result<Request, io::Error>> + Unpin, mut stdout: impl Sink<Response, Error = io::Error> + Unpin, ) -> Result<(), Box<dyn Error>> { Is there a standard/easy way to adapt a function to a sink for printing and/or transformation? eg. something like: connect(.., .., sink::from_function(|r| match r { Ok(response) => println!("received a

Rust futures — adapting a function as a Sink

烈酒焚心 提交于 2021-02-08 11:11:59
问题 I have something similar to the tokio connect example with a method that accepts a sink: pub async fn connect( addr: &SocketAddr, mut stdin: impl Stream<Item = Result<Request, io::Error>> + Unpin, mut stdout: impl Sink<Response, Error = io::Error> + Unpin, ) -> Result<(), Box<dyn Error>> { Is there a standard/easy way to adapt a function to a sink for printing and/or transformation? eg. something like: connect(.., .., sink::from_function(|r| match r { Ok(response) => println!("received a

Cannot borrow as mutable more than once error in a loop

大城市里の小女人 提交于 2021-02-08 08:58:40
问题 I am working on leetcode problem #83 "Remove Duplicates from Sorted List", but I'm stuck on this borrow checker issue. The ListNode struct is given by the problem so it cannot be changed. I have tried restructuring the loop and if statement, but I haven't found a working solution. What I am trying to do: // Definition for singly-linked list. #[derive(PartialEq, Eq, Debug)] pub struct ListNode { pub val: i32, pub next: Option<Box<ListNode>>, } impl ListNode { #[inline] fn new(val: i32) -> Self

How to modify each section of a vector in multiple threads [duplicate]

╄→尐↘猪︶ㄣ 提交于 2021-02-08 08:01:30
问题 This question already has answers here : How do I pass disjoint slices from a vector to different threads? (1 answer) How can I pass a reference to a stack variable to a thread? (1 answer) How to get mutable references to two array elements at the same time? (6 answers) Closed 2 years ago . I have a vector of u8 and I need to fill this vector with values that can be computed in parallel: let vector: Vec<u8> = vec![0, len]; Given n_threads threads, each thread can take care of a section of the

How to modify each section of a vector in multiple threads [duplicate]

不打扰是莪最后的温柔 提交于 2021-02-08 08:01:27
问题 This question already has answers here : How do I pass disjoint slices from a vector to different threads? (1 answer) How can I pass a reference to a stack variable to a thread? (1 answer) How to get mutable references to two array elements at the same time? (6 answers) Closed 2 years ago . I have a vector of u8 and I need to fill this vector with values that can be computed in parallel: let vector: Vec<u8> = vec![0, len]; Given n_threads threads, each thread can take care of a section of the

Comparing functions for equality in Rust

本小妞迷上赌 提交于 2021-02-08 07:43:21
问题 I have a function which takes a number as an argument, and then returns a function based on the number. Depending on many different things, it might return any of ~50 functions, and the cases for which one it should return get pretty complicated. As such, I want to build some tests to make sure the proper functions are being returned. What I have so far looks roughly like this. fn pick_a_function(decider: u32) -> fn(&mut SomeStruct) { match decider { 1 => add, 2 => sub, _ => zero, } } fn add