pattern-matching

Why does _ destroy at the end of statement?

大憨熊 提交于 2020-01-23 05:30:08
问题 I've seen a few other questions and answers stating that let _ = foo() destroys the result at the end of the statement rather than at scope exit, which is what let _a = foo() does. I am unable to find any official description of this, nor any rationale for this syntax. I'm interested in a few inter-twined things: Is there even a mention of it in the official documentation? What is the history behind this choice? Is it simply natural fall-out from Rust's binding / destructuring rules? Is it

Match product dimensions with regular expression

Deadly 提交于 2020-01-23 02:12:26
问题 I am trying to match length width and height with a regular expression. I have the following cases Artikelgewicht3,7 Kg Produktabmessungen60,4 x 46,5 x 42 cm or Artikelgewicht3,7 Kg Produktabmessungen60 x 46 x 42 or Artikelgewicht3,7 Kg Produktabmessungen60 x 46 The second case can be matched with (\d+) x (\d+) x (\d+) , which works fine. I further tried to match the first and the third case with (\d+)(\\,\d+)? x (\d+)(\\,\d+)? x (\d+)(\\,\d+)? . Any suggestions what I am doing wrong? 回答1:

F# Records: Dangerous, only for limited use, or well used functionality?

末鹿安然 提交于 2020-01-22 19:52:08
问题 So have gotten to record in my F# journey and at first they seem rather dangerous. At first this seemed clever: type Card = { Name : string; Phone : string; Ok : bool } let cardA = { Name = "Alf" ; Phone = "(206) 555-0157" ; Ok = false } The idea that the cardA is patten matched with Card. Not to mention the simplified pattern matching here: let withTrueOk = list |> Seq.filter (function | { Ok = true} -> true | _ -> false ) Problem is: type Card = { Name : string; Phone : string; Ok : bool }

Invalid regular expression error

戏子无情 提交于 2020-01-21 12:08:43
问题 I'm trying to retrieve the category part this string " property_id=516&category=featured-properties ", so the result should be "featured-properties", and I came up with a regular expression and tested it on this website http://gskinner.com/RegExr/, and it worked as expected, but when I added the regular expression to my javascript code, I had a "Invalid regular expression" error, can anyone tell me what is messing up this code? Thanks! var url = "property_id=516&category=featured-properties"

Invalid regular expression error

一个人想着一个人 提交于 2020-01-21 12:08:10
问题 I'm trying to retrieve the category part this string " property_id=516&category=featured-properties ", so the result should be "featured-properties", and I came up with a regular expression and tested it on this website http://gskinner.com/RegExr/, and it worked as expected, but when I added the regular expression to my javascript code, I had a "Invalid regular expression" error, can anyone tell me what is messing up this code? Thanks! var url = "property_id=516&category=featured-properties"

Is it possible to create a mutable value of a mutable reference in a pattern?

跟風遠走 提交于 2020-01-21 11:10:33
问题 When pattern-matching, you can specify that you'd like to get a mutable reference to the contained value by using ref mut : let mut score = Some(42); if let Some(ref mut s) = score { &mut s; } However, the inner value is not mutable: error[E0596]: cannot borrow immutable local variable `s` as mutable --> src/main.rs:4:14 | 4 | &mut s; | ^ | | | cannot reborrow mutably | try removing `&mut` here I tried to add in another mut , but that was not valid: if let Some(mut ref mut s) = score { &mut s

Is it possible to create a mutable value of a mutable reference in a pattern?

空扰寡人 提交于 2020-01-21 11:08:23
问题 When pattern-matching, you can specify that you'd like to get a mutable reference to the contained value by using ref mut : let mut score = Some(42); if let Some(ref mut s) = score { &mut s; } However, the inner value is not mutable: error[E0596]: cannot borrow immutable local variable `s` as mutable --> src/main.rs:4:14 | 4 | &mut s; | ^ | | | cannot reborrow mutably | try removing `&mut` here I tried to add in another mut , but that was not valid: if let Some(mut ref mut s) = score { &mut s

Pattern match on value of Either inside a for comprehension?

荒凉一梦 提交于 2020-01-21 11:06:43
问题 I have a for comprehension like this: for { (value1: String, value2: String, value3: String) <- getConfigs(args) // more stuff using those values } getConfigs returns an Either[Throwable, (Seq[String], String, String)] and when I try to compile I get this error: value withFilter is not a member of Either[Throwable,(Seq[String], String, String)] How can I use this method (that returns an Either ) in the for comprehension? 回答1: Like this: for { tuple <- getConfigs() } println(tuple) Joking

Pattern match on value of Either inside a for comprehension?

[亡魂溺海] 提交于 2020-01-21 11:04:09
问题 I have a for comprehension like this: for { (value1: String, value2: String, value3: String) <- getConfigs(args) // more stuff using those values } getConfigs returns an Either[Throwable, (Seq[String], String, String)] and when I try to compile I get this error: value withFilter is not a member of Either[Throwable,(Seq[String], String, String)] How can I use this method (that returns an Either ) in the for comprehension? 回答1: Like this: for { tuple <- getConfigs() } println(tuple) Joking

Pattern Match “return” value

廉价感情. 提交于 2020-01-21 11:03:51
问题 Why is it not possible to chain pattern matching constructs? For instance, the following is legal, if nonsensical, val a = ADT(5) val b = a match { case ADT(a) if a > 4 => ADT(a * 3) case ADT(a) => ADT(a + 1) } b match { case ADT(a) if a > 13 => doSomething(a) case _ => {} } but the following is not: a match { case ADT(a) if a > 4 => ADT(a * 3) case ADT(a) => ADT(a + 1) } match { case ADT(a) if a > 13 => doSomething(a) case _ => {} } I suspect it's because I shouldn't be doing it in the first