These are chat archives for rust-lang/rust

25th
Jan 2018
Kelly Thomas Kline
@kellytk
Jan 25 2018 04:54
How should the match block beginning on line 8 of https://play.integer32.com/?gist=c91893553ebca3838058653d19ffd629&version=stable be modified such that the assignment to first occurs if strings has at least 1 value?
or return true
Kelly Thomas Kline
@kellytk
Jan 25 2018 05:04
Thanks @red75prime
Is it reasonable practice to use match branches to change state in the match's containing scope as I am here?
red75prime
@red75prime
Jan 25 2018 05:14
@kellytk In this case probably no. The construction below is more idiomatic.
    let first = 
        match strings.first() {
            Some(f) => f,
            None => return true,
        };
but sometimes it is useful
Kelly Thomas Kline
@kellytk
Jan 25 2018 05:21
@red75prime Oh, thanks. Out of curiosity, is it possible to make first a const where effectively the match part of the assignment musn't be None? What I'm thinking is that there would be an additional qualifier declared on first and the None match branch would need to return something to assign to
red75prime
@red75prime
Jan 25 2018 05:35
I'm not sure I understood you. first is already immutable in your example. Compiler will not allow the use of unassigned variable, so you need to assign something to first or to return early.
Kelly Thomas Kline
@kellytk
Jan 25 2018 06:12
@red75prime I'm still confused by what implication the construction None => return true has on the first assignment. Why does the print not execute in https://play.integer32.com/?gist=41b2eee36c17f17bdeafa715b4a87ac3&version=stable
Kelly Thomas Kline
@kellytk
Jan 25 2018 06:22
I was (incorrectly) expecting "first " to be printed
red75prime
@red75prime
Jan 25 2018 08:16
@kellytk You have .get(10) in there. There's no eleventh element in the vector you pass.
Kelly Thomas Kline
@kellytk
Jan 25 2018 08:17
@red75prime That's intentional, to trigger the None branch
red75prime
@red75prime
Jan 25 2018 08:20
None branch performs early return from the function.
You can also substitute empty string if the index is outside the vector None => "",
Kelly Thomas Kline
@kellytk
Jan 25 2018 08:22
None branch performs early return from the function.
Does that mean no assignment to first occurs?
red75prime
@red75prime
Jan 25 2018 08:22
yes
Kelly Thomas Kline
@kellytk
Jan 25 2018 08:23
What's the return true/false you used in the branch?
Kelly Thomas Kline
@kellytk
Jan 25 2018 08:27
Oh so None itself isn't the early return, it's the condition containing the early return. Is that correct?
red75prime
@red75prime
Jan 25 2018 08:27
yes
Kelly Thomas Kline
@kellytk
Jan 25 2018 08:28
Wouldn't the return bool (true or false) be assigned to first?
Patrick Elsen
@xfbs
Jan 25 2018 08:28
return returns from the function, not from the match clause
Kelly Thomas Kline
@kellytk
Jan 25 2018 08:29
Oh
Patrick Elsen
@xfbs
Jan 25 2018 08:30
If you did
 let first = match strings.get(10) {
    Some(_) => true,
    None => false,
 };
Then the match would return true if there is a 10th element in strings, and false if there isn't.
But the return false statement just stops everything and makes the valid_strings() function return early. :)
Kelly Thomas Kline
@kellytk
Jan 25 2018 08:31
I should have put that together, thanks @xfbs and @red75prime :-)
laurent bernabé
@loloof64
Jan 25 2018 09:56
~Thanks @xfbs. I'll also think about using num.to_u32.~ (Oups, I did not read your whole message)
let a = 10i16;
let b:u32 = a as u32;
Kelly Thomas Kline
@kellytk
Jan 25 2018 12:27
Should a function which parses a string and returns a struct if the string's contents are well-formed have a return type of Option or Result?
Denis Lisov
@tanriol
Jan 25 2018 12:36
@kellytk How about implementing FromStr?
Zakarum
@omni-viral
Jan 25 2018 12:41
@kellytk In general you should prefer Result if your function can result in "success" or "failure"
prefer Option if your function results in "something" or "nothing"
Kelly Thomas Kline
@kellytk
Jan 25 2018 12:46
Thanks @tanriol and @omni-viral
ocschwar
@ocschwar_twitter
Jan 25 2018 20:57
lo, all. I have a question about Diesel: is there a way to derive the objects from an existing SQL schema?
Denis Lisov
@tanriol
Jan 25 2018 20:59
Sounds like diesel print-schema
ocschwar_twitter @ocschwar_twitter tries it. WOrks! THanks!