These are chat archives for rust-lang/rust

15th
Oct 2015
waat
@Waateur
Oct 15 2015 12:01
Hello, I'm a long time lurking. And i finally have time learn a bit of rust.
I have can issu with the try! macro. I can't wrap my head arround this macro.
So I come with the hope to find a little help :)
here is my rust code
use std::env;
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
use std::fs::File;
use std::path::Path;
fn main() {
    let args: Vec<String> = env::args().collect();
    let path = Path::new(&args[1]);
    let file = try!(path.file_name())//.unwrap();
    //println!("{}", file.to_str().unwrap());
}
and here is the error :
<std macros>:1:1: 6:48 note: in expansion of try!
src/main.rs:10:16: 10:38 note: expansion site
<std macros>:3:1: 3:39 help: run `rustc --explain E0308` to see a detailed explanation
<std macros>:3:49: 4:12 error: mismatched types:
 expected `core::option::Option<&std::ffi::os_str::OsStr>`,
    found `core::result::Result<_, _>`
(expected enum `core::option::Option`,
    found enum `core::result::Result`) [E0308]
<std macros>:3 $ crate:: result:: Result:: Ok ( val ) => val , $ crate:: result:: Result::
<std macros>:4 Err ( err ) => {
<std macros>:1:1: 6:48 note: in expansion of try!
src/main.rs:10:16: 10:38 note: expansion site
<std macros>:3:49: 4:12 help: run `rustc --explain E0308` to see a detailed explanation
<std macros>:5:8: 6:42 error: mismatched types:
 expected `()`,
    found `core::result::Result<_, _>`
(expected (),
    found enum `core::result::Result`) [E0308]
<std macros>:5 return $ crate:: result:: Result:: Err (
<std macros>:6 $ crate:: convert:: From:: from ( err ) ) } } )
<std macros>:1:1: 6:48 note: in expansion of try!
src/main.rs:10:16: 10:38 note: expansion site
<std macros>:5:8: 6:42 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to 3 previous errors
Could not compile `arpa_reader`.
I used to unwrap path.file_name() and it works
but I think that is the job for try! isn't it ?
also when i try to open the std::File or read_to_string I still have the same issue
waat
@Waateur
Oct 15 2015 12:07
and I don't think using unwrap() everywhere is the key
waat
@Waateur
Oct 15 2015 12:13
am i wrong ?
panicbit
@panicbit
Oct 15 2015 12:59
The thing about try! is that it either unwraps() if the value is Ok() or it returns the Err() (Ok and Err are variants of the Result enum type). So the Problem is, that the main function has no return value, but the try! macro wants to be able to return something of the Result type.
Oh wait. Another problem is, that try! only accepts the Result type, but you are passing in an Option type.
panicbit
@panicbit
Oct 15 2015 13:07
You probably won't need the file_name method anyway. Most methods accept &Paths. If you want to print a filename you can use path.display():
use std::path::Path;

fn main() {
    let path = Path::new("foo/bar");
    println!("{}", path.display());
}
waat
@Waateur
Oct 15 2015 13:20
Thank you a lot @panicbit ! It was the return thing that I missed ! It's all clear now !
Johan Larsson
@JohanLarsson
Oct 15 2015 13:33
panicbit avoids panic once again