These are chat archives for rust-lang/rust

14th
Jan 2019
Zakarum
@omni-viral
Jan 14 09:16
+1 for using rustdoc to create birdseye overview
Ogulcan Erduran
@erduranogulcan
Jan 14 14:26
I checked the docs and web topics but I could not find correct answer. Is there try catch blocks in 2018 edition? There are topics about Result<String,Err> but I could not do that correctly. Can someone help? how can i use try catch blocks ? Like ;
Try{ blabla } catch { print(err) }
Ingvar Stepanyan
@RReverser
Jan 14 14:29
No, it's just an idea for which keyword was reserved
Mr Ceperka
@mrceperka
Jan 14 15:37
@erduranogulcan try pattern matching with Result, it's good.
thojest
@thojest
Jan 14 15:47
is the usage of unwrap() bad coding style because of missing error handling, or is it ok to use when one is really sure that this cannot fail ?
Ingvar Stepanyan
@RReverser
Jan 14 15:48
both :)
if you're absolutely sure (e.g. this is a Result that you just created from Ok), then it's fine
thojest
@thojest
Jan 14 15:49
:) ok
Tim Robinson
@1tgr
Jan 14 15:49
I guess the question would be, if you know it can't fail, why is it an Option or a Result at all?
Michal 'vorner' Vaner
@vorner
Jan 14 15:50
@thojest unwrap will panic if there's an error. And panic should usually be used when the application finds a bug in itself ‒ so assert panics, tests panic, access outside of array panic. So if getting Err (or None) there is a bug, then unwrap (or expect). If it can be caused by external circumstances (stupid user, broken files on disk, lost connectivity), then handle it in other ways
@1tgr Many reasons. Maybe you have something you need to consume in Drop, but that one gives you only &mut self access. So you put it to Option, but you're sure you're going to take it out only in the destructor.
Or maybe you want to do let ip: IpAddr = "127.0.0.1".parse().unwrap(); ‒ you know that this string is valid IP address so that parse won't fail. But that parse don't know it, so it has to return Result
thojest
@thojest
Jan 14 15:53
@vorner thx for the examples
thojest
@thojest
Jan 14 16:18
another question
how do i define bounds for a function where T is an Enum?
thojest
@thojest
Jan 14 16:26
ok, think I got it
trait bounds has nothing to do with enums i guess
Michal 'vorner' Vaner
@vorner
Jan 14 16:28
If something is a struct, primitive type, enum or union is independent concept from traits. Traits work on all of them the same way.
moofone
@moofone
Jan 14 16:57
does the grin miner start on a random nonce? Wondering if duplicate work will be an issue
Victor Lopes
@vlopes11
Jan 14 20:42
Hello Rustaceans. Is it possible to have a enum element that receives an instance of another enum? Example:
enum A {
    X,
}

enum B {
    X(A::X),
}
Ingvar Stepanyan
@RReverser
Jan 14 20:43
yes, but in the definition it has to be a type not an instance
or do you want to have it limited just to that one instance?
Victor Lopes
@vlopes11
Jan 14 20:44
Not sure if I understood. Could be something like this (this is a generic example)
enum A {
    X,
    Y,
}

enum B {
    X(A::X),
    Y(A::Y),
}

enum C {
    X(A::X),
    Y(A::Y),
}
Ingvar Stepanyan
@RReverser
Jan 14 20:45
No, you just need to define a variant that accepts the entire type
enum A {
  X,
  Y,
}

enum B {
  A(A),
}

enum C {
  A(A),
}
Victor Lopes
@vlopes11
Jan 14 20:46
Oh, understood. Thanks
Alex Hoeing
@DCBN
Jan 14 21:47
Hey! Someone who can help me think differently? I have been doing a lot of frontend developing, especially in Javascript and there is just some things where what I have in mind (technically) works out in Rust.
If you assume you have a collection of files and you want to organize these, in Javascript I would simply create an object with the filetype as the key and an array with the items fitting into this collection.
Something tells this isn't how you would do it in Rust?
Farhan Ahmed
@IMacronaut_twitter
Jan 14 22:28

I am trying to do is add utility methods/functions via a trait to a type that is part of an external crate, is this possible? e.g. I want to add helper functions (not provided by the crate) to the DateTime<Tz> type of the chrono crate using a custom trait.

This works so long as the trait, its impl and the functions that use the methods from the trait are in the same file. However, fails when I try to use the methos provided by the trait from lib.rs with the error "no method named tommorow found for type chrono::datetime::DateTime<chrono::offset::utc::Utc> in the current scope".

Am doing the right thing (that is idiomatic for Rust)?
toxicafunk
@toxicafunk
Jan 14 22:34
One restriction to note with trait implementations is that we can implement a trait on a type only if either the trait or the type is local to our crate.
...
Tim Robinson
@1tgr
Jan 14 22:36
@DCBN no, but in Rust you have a HashMap that can associate any key type with anything
(provided the key implements Hash and Eq, which most stuff does)