These are chat archives for rust-lang/rust

30th
Dec 2018
Matthew de Detrich
@mdedetrich
Dec 30 2018 00:13
Yeah I just wanted to know if by ugly people mean aesthetically ugly or worse
Matthew de Detrich
@mdedetrich
Dec 30 2018 00:23
@Ichoran The thing is for serialization the fieldnames do matter
Ichoran
@Ichoran
Dec 30 2018 00:24
Yes, though if you're using serde you can override the names.
Rohan Jain
@crodjer
Dec 30 2018 05:33
Going through the rust book, recursive data types with Box section: https://doc.rust-lang.org/book/ch15-01-box.html#using-boxt-to-get-a-recursive-type-with-a-known-size. I see that use List::{Cons, Nil}; fails with 2018 edition. Any way around this error?
1 | / enum List {
2 | |     Cons(i32, Box<List>),
3 | |     Nil,
4 | | }
  | |_- not an extern crate passed with `--extern`
5 |
6 |   use List::{Cons, Nil};
  |       ^^^^
  |
note: this import refers to the enum defined here
 --> src/bin/ch_15.rs:1:1
  |
1 | / enum List {
2 | |     Cons(i32, Box<List>),
3 | |     Nil,
4 | | }
  | |_^
This error goes away when I remove the edition = "2018" configuration from Cargo.toml.
Tim Robinson
@1tgr
Dec 30 2018 06:11
use self::List::{Cons, Nil};
Rohan Jain
@crodjer
Dec 30 2018 06:28
@1tgr Thanks. That worked!
Rohan Jain
@crodjer
Dec 30 2018 06:36
It seems to be fixed in the rust book as well. But the fix uses crate instead of self:
use crate::List::{Cons, Nil};
Tim Robinson
@1tgr
Dec 30 2018 09:07
self is relative to the current module, crate is absolute starting at the root of the current crate
As you have only one module in your crate they mean the same thing here
laurent bernabé
@loloof64
Dec 30 2018 19:07
Hi everyone :smile: Is there a simple way to prevent an external function to panic and crash the program ? Indeed, I am using the UCI crate. It has a constructor Engine::new(path_str) which panic is the argument is wrong. (Its signature is pub fn new(path: &str) -> Result<Engine> and uses some ? in order to throw exception in some of its code). But I did not find a way to prevent it from panicing.
What I tried
pub fn new_from_path(path: &str) -> Result<Computer, String> 
    {
        let engine = Engine::new(path);
        match engine {
            Ok(engine) => Ok(
                Computer { engine }
            ),
            Err(error) => Err(match error {
                EngineError::Io(_) => "Could not open engine file !".to_owned(),
                EngineError::UnknownOption(opt) => format!("Unknown engine option {} !", opt),
            })
        }
    }
But the program panics before the match expression.
toxicafunk
@toxicafunk
Dec 30 2018 19:26
have u tried wrapping it in a try!?
laurent bernabé
@loloof64
Dec 30 2018 19:34
No thanks :smile: I'll look at an example :smile:
laurent bernabé
@loloof64
Dec 30 2018 19:55
I tried the following, but it still panics as before :
pub fn new_from_path(path: &str) -> Result<Computer, EngineError> 
    {
        let engine = try!(Engine::new(path));
        Ok(Computer { engine })
    }
Invokes a closure, capturing the cause of an unwinding panic if one occurs.
laurent bernabé
@loloof64
Dec 30 2018 20:11
Thank you :smile: I'll look at it right now :smile:
laurent bernabé
@loloof64
Dec 30 2018 20:18
It seems that with catch_unwind I can't get the result of call to Engine::new(path) into the engine variable. Trying to change the code to something like
let engine = panic::catch_unwind(|| {
            Engine::new(path);
        });
It returns a Result with Empty as Left value
Ichoran
@Ichoran
Dec 30 2018 23:38
I think you need to figure out why the external call is panicking. You can't just have things be broken and proceed as normal.
I don't know why you can't get a new engine, but if that's panicking, you don't just want to ignore that you didn't actually get a proper engine, do you?