These are chat archives for rust-lang/rust

10th
Nov 2017
xhallix
@xhallix
Nov 10 2017 07:07

I have a struct which returns a writer
I'm having some trouble getting the lifetime right for this.

I understand why the compile complains - but I'm not sure how to solve this in that case


pub struct GWXmlWriter();

impl <'a> GWXmlWriter {
    ///
    /// Creates a new writer
    ///
    pub fn new<'a>(file_name : String) -> xml::EventWriter<&'a mut std::fs::File> {
        let mut file = File::create(&file_name).unwrap();
        let mut writer = EmitterConfig::new().perform_indent(true).create_writer(&mut file);
        writer
    }
}

Error error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope --> src/lib.rs:44:16 | 40 | impl <'a> GWXmlWriter { | -- first declared here ... 44 | pub fn new<'a>(file_name : String) -> xml::EventWriter<&'a mut std::fs::File> { | ^^ lifetime 'a already in scope

Jonas Platte
@jplatte
Nov 10 2017 07:11
@xhallix You are trying to return a referene to a variable local to new, you can't do that
xhallix
@xhallix
Nov 10 2017 07:12
I need to make that variable owned - correct?
Jonas Platte
@jplatte
Nov 10 2017 07:13
Yeah something along those lines
xhallix
@xhallix
Nov 10 2017 07:14
so something like writer.clone() ?
Jonas Platte
@jplatte
Nov 10 2017 07:14
Ah so EmitterConfig::create_writer has a generic argument that just needs to impl Writer
Why are you passing it a reference to file, instead of giving it ownership of file?
xhallix
@xhallix
Nov 10 2017 07:17
I tried file.try_clone().unwrap() and pass it an owned file - but this did not make things better
Jonas Platte
@jplatte
Nov 10 2017 07:19
I've never heard about try_clone... But you shouldn't have to either way. You should be able to just move it:
pub fn new(file_name: String) -> xml::EventWriter<File> {
    let file = File::create(&file_name).unwrap();
    EmitterConfig::new().perform_ident(true).create_writer(file)
}
xhallix
@xhallix
Nov 10 2017 07:22
yes you are right - not returning the reference is the solution
Jonas Platte
@jplatte
Nov 10 2017 07:22
xhallix
@xhallix
Nov 10 2017 07:23
I did 2-3 times but I think I just need some more practices to get used to this : )
Jonas Platte
@jplatte
Nov 10 2017 07:23
Okay
Sherab Giovannini
@Shaddy
Nov 10 2017 11:08
hey guys, I'm getting build error with clap because of message: String::from_utf8(buf).unwrap_or_default()
seems that Result doesn't have unwrap_or_default
which belongs to Option instead
Max Frai
@max-frai
Nov 10 2017 11:09
@Shaddy No, Result has such method
Sherab Giovannini
@Shaddy
Nov 10 2017 11:09
:/
Max Frai
@max-frai
Nov 10 2017 11:09
Show error
Sherab Giovannini
@Shaddy
Nov 10 2017 11:10
I'm doing a dummy test, with the same case
fn test_function() -> Result<u8, u8> {
    Ok(1)
}

fn main() {
    if let Ok(number) = test_function() {
        println!("number: {}", number);
    }

    let number = test_function().unwrap_or_default();

    println!("{}", number);
}
error: no method named `unwrap_or_default` found for type `std::result::Result<u8, u8>` in the current scope
  --> src\main.rs:10:34
   |
10 |     let number = test_function().unwrap_or_default();
   |
The original error was:
error: no method named `unwrap_or_default` found for type `std::result::Result<std::string::String, std::string::FromUtf8Error>` in the current scope
    --> C:\Users\Sherab\.cargo\registry\src\github.com-1ecc6299db9ec823\clap-2.27.1\src\app\parser.rs:1364:49
     |
1364 |                 message: String::from_utf8(buf).unwrap_or_default(),
Max Frai
@max-frai
Nov 10 2017 11:13
What version of rust do you use?
Sherab Giovannini
@Shaddy
Nov 10 2017 11:17
active toolchain
----------------

stable-x86_64-pc-windows-msvc (default)
rustc 1.21.0 (3b72af97e 2017-10-09)
maybe is Windows?
Jonas Platte
@jplatte
Nov 10 2017 11:20
@Shaddy So you are getting an error message outside of your own code, with rust 1.21.0? :O
Sherab Giovannini
@Shaddy
Nov 10 2017 11:22
I'm just getting the error building the project yep
Jonas Platte
@jplatte
Nov 10 2017 11:23
Huh.. What does rustup toolchain list say?
Is stable 1.21.0 the only rust you have installed?
Sherab Giovannini
@Shaddy
Nov 10 2017 11:23
PS C:\GoogleFS\byteheed\conveyor> rustup toolchain list
stable-x86_64-pc-windows-msvc (default)
nightly-x86_64-pc-windows-msvc
no
I usually use nightly, but to ensure its not an nightly problem
I just setd as default stable
red75prime
@red75prime
Nov 10 2017 11:27
Does the error persist after clean rebuild?
Sherab Giovannini
@Shaddy
Nov 10 2017 11:27
yes
my cargo binary is kind of obsolete (0.13)
I'm updating..
something is wrong with my current toolchain, I'm going to freshly resinstall everything...
Denis Lisov
@tanriol
Nov 10 2017 11:30
Does rustup override list show anything?
Sherab Giovannini
@Shaddy
Nov 10 2017 11:31
nope
Sherab Giovannini
@Shaddy
Nov 10 2017 11:36
wow, I've seen a coflicting totally outdated cargo and rustc installation... sorry for the waste of time :/
had one 1.13 rustc version lost in the environment paths
rustup was pointing to correct path, but cargo didn't so the compiler taken was the outdated despite rustup was ok
Hans W. Uhlig
@huhlig
Nov 10 2017 19:37
Is it possible to use a numeric primitive as a generic specifier and use that to size an embedded array?
Nikolay Kim
@fafhrd91
Nov 10 2017 19:58