These are chat archives for rust-lang/rust

14th
Mar 2019
John
@onFireForGod_gitlab
Mar 14 00:48
are there libraries in rust that will determine weather a day is daylight or standard given a time zone for example 'America/Los_Angeles’ ?
I need to convert an excel spreadsheet that has a time series data in PST and PDT, I could write an algorithm that determines when to change the offset, but it will be nice if there is a library that does that already
got an answer from the chrono chat:https://github.com/chronotope/chrono-tz
John
@onFireForGod_gitlab
Mar 14 01:08
What is the difference between cargo run —bin and cargo run —example
Brian Knapp
@knappador
Mar 14 03:21
What's something to read about creating nice API's in Rust? I'm interested in how to alleviate ownership and type friction when presenting a user-facing API
John
@onFireForGod_gitlab
Mar 14 03:27
I have a absolute path for a file that I want to open but I keep getting this:
    Finished release [optimized] target(s) in 0.37s
     Running `target/release/dataseed`
thread 'main' panicked at 'csv file not found: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/libcore/result.rs:997:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
   1: std::sys_common::backtrace::_print
   2: std::panicking::default_hook::{{closure}}
   3: std::panicking::default_hook
   4: std::panicking::rust_panic_with_hook
   5: std::panicking::continue_panic_fmt
   6: rust_begin_unwind
   7: core::panicking::panic_fmt
   8: core::result::unwrap_failed
   9: dataseed::main
  10: std::rt::lang_start::{{closure}}
  11: std::panicking::try::do_call
  12: __rust_maybe_catch_panic
  13: std::rt::lang_start_internal
  14: main
let mut reader = csv::Reader::from_reader(File::open("‎⁨/Users/johnkarasev/nwpcc-projects/demand.csv").expect("csv file not found"));
I checked the path with cat and it works
matrixbot
@matrixbot
Mar 14 03:31
bspeice Capitalization issues maybe? Rust doesn't do anything fancy, it just delegates to the operating system for opening the file.
John
@onFireForGod_gitlab
Mar 14 03:32
no there is no capitalization issues
like I said, I used cat to see if it will open the file and it did. cat uses the same system call
Brian Knapp
@knappador
Mar 14 03:34
@onFireForGod_gitlab try to construct a PathBuf?
John
@onFireForGod_gitlab
Mar 14 03:35
why should I need to do that? It should be able to do it just from the string lol
I checked with examples thats how its done
well let me try it might not like the slashes
John
@onFireForGod_gitlab
Mar 14 03:52
still does not work
using mojave
mac
    let mut pathb: PathBuf = PathBuf::new(); 
    pathb.push("r/Users"); 
    pathb.push("johnkarasev"); 
    pathb.push("nwpcc-projects"); 
    pathb.push("demand.csv"); 
    let mut reader = csv::Reader::from_reader(File::open(pathb)?);
Error: Os { code: 2, kind: NotFound, message: "No such file or directory” }
just started working .
dont konw what happened
mac lol
Brian Knapp
@knappador
Mar 14 04:03
Satan's OSj
Ingvar Stepanyan
@RReverser
Mar 14 17:01
@onFireForGod_gitlab your path isn't absolute, so it would work only if you start from a specific folder
Unless you meant just /Users and not r/Users
Ash
@ashthespy
Mar 14 23:22
Basic question - how does on go from a Result -> Option while throwing the error?
I can't find a Result::ok_or() similar to Option::ok_or()
Joey
@OddCoincidence
Mar 14 23:24
Some(result?)?
(by throwing the error do you mean returning it?)
or if you meant throwing away the error, there's Result::ok()
Ash
@ashthespy
Mar 14 23:26
? does the trick!
But that will panic on the Err correct?
Joey
@OddCoincidence
Mar 14 23:27
No, it'll return it
Basically the same as:
match err {
    Ok(value) => value,
    Err(err) => return Err(err)
}
Ash
@ashthespy
Mar 14 23:28
Okay! Shall give it a go!
the trait bound(): std::convert::From<serde_json::Error>is not satisfied Will just do my usual match
I was hoping to start writing more clean code..
Joey
@OddCoincidence
Mar 14 23:34

It's perfectly fine to write things a bit more verbosely while you are getting used to it.

the trait bound(): std::convert::From<serde_json::Error>is not satisfied Will just do my usual match

this error means that 1. the type of error of the result you applied ? to and the one the function returns are different. rust will convert it for you if there is an appropriate From impl, which is where that error message is coming from

Here's the relevant section of The Book, which explains it 10x better than I can: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
Ash
@ashthespy
Mar 14 23:38
I see my issue - my function doesn't have a Result return type..
Joey
@OddCoincidence
Mar 14 23:39
That'll do it :)
Ash
@ashthespy
Mar 14 23:40
self.op_res = match res {
                Ok(value) => {
                    info!(
                        "Resolved {:?}",
                        value.uri
                    );
                    Some(value)
                }
                Err(e) => {
                    error!("Parsing Error: {:?}", e);
                    None
                }
            };
That is what I do now - If I were to loose the prints, can I make this less verbose?
Joey
@OddCoincidence
Mar 14 23:41
self.op_res = res.ok();
Ash
@ashthespy
Mar 14 23:42
That is where I started :-)
there isn't an ok_or() for Results right?
Joey
@OddCoincidence
Mar 14 23:44
There isn't - what would that method do?
Ash
@ashthespy
Mar 14 23:48
Convert an Ok() to Some() or propagate the Err
Joey
@OddCoincidence
Mar 14 23:48
By propagate error, you mean return it?