These are chat archives for rust-lang/rust

23rd
Nov 2016
mchapala
@mchapala
Nov 23 2016 01:20
I am learning rust and going through the rustbyexample webiste. I copied the code from file open example. For some reason the sample code shows as if the file is empty but the cat command correctly lists the contents
pub fn read_file(path:&Path, buffer:&mut String) -> Result<usize,io::Error> {

    let path2 = Path::new("/Volumes/disk0s4/development/RustAlarm/src/config/config.toml");
    let mut file = File::open(&path2).unwrap();
    let resp =  file.read_to_string(&mut buffer.to_string());
    match resp {
        Err(why) => panic!("couldn't read {}", why),
        Ok(_) => print!("{} contains:\n{}", path.display(), buffer),
    }
    resp

}
any idea what I might be doing wrong
Patrick Greene
@bytebuddha
Nov 23 2016 05:13
I haven't tested this but I'm sure you need to remove the to_string() call because your passing read_file_to_string a new string copied from buffer intead of buffer itself.
Also you can remove the line with let path2 and pass path directly to File::open.
mchapala
@mchapala
Nov 23 2016 12:17
Thanks @bytebuddha. Yeah the buffer.to_string() was the issue.
Nehal Hasnayeen
@Hasnayeen
Nov 23 2016 13:25
can string be added to &str
Ingvar Stepanyan
@RReverser
Nov 23 2016 13:45
@Hasnayeen &str is just slice of memory, so no
you need to convert it to owned string first
Pavel Meledin
@btbvoy
Nov 23 2016 18:36

Hi everyone, I have the following code:

#[derive(Debug)]
pub struct A {
    s: u8
}

impl A {
    pub fn new () -> A {
        A { s: 1 }
    }

    pub fn play(&mut self, events: Vec<u8>) -> &mut A {
        self
    }
}

which I’m running like this:

let player =
        A::new()
        .play(vec![]);
    println!("{:?}", player);

compiler fails with:

error: borrowed value does not live long enough
   --> src/main.rs:28:9
    |
28  |         A::new()
    |         ^^^^^^^^ temporary value created here
29  |         .play(vec![]);
    |                      - temporary value only lives until here
...
116 | }
    | - temporary value needs to live until here
    |
    = note: consider using a `let` binding to increase its lifetime

Some time ago I was catched with similar issue but at that moment I missed lifetime parameter but there is no explicit lifetime parater here. Might be of course it’s the root of the issue but I don’t really get what is wrong here. Please help

Pavel Meledin
@btbvoy
Nov 23 2016 18:45
the same example in play.rust-lang.org — https://is.gd/m9OrX0
Jonas Platte
@jplatte
Nov 23 2016 18:48
@btbvoy I think the only possibility you have here is to do what the compiler is suggesting and bind player to A::new() before calling play.
Pavel Meledin
@btbvoy
Nov 23 2016 18:48
yes, I did and it does work. but I would like to understand why it doesn’t work in such a way I posted above
just for understanding how it works under the hood
Jonas Platte
@jplatte
Nov 23 2016 18:51
So when you call A::new(), you get an A (it returns a value). You can easily bind that to a variable and use it for the scope of that variable. However, when you call play on the temporary (on the expression A::new()) then the value returned by A::new() still needs to have a lifetime. It's not actually bound to anything with a scope beyond the let player = ...; statement, so that becomes its lifetime.
actually, I think the lifetime doesn't go beyond the expression A::new().play(vec![])
It makes sense if you think about the types.
A variable of type &mut A (as player would be here) is just a reference to an actual value. A reference, mutable or not, can never extend the lifetime of a value.
Pavel Meledin
@btbvoy
Nov 23 2016 18:56
so the issue is because A::new() returns just an instance of A but when I do call play on A return result is a reference and as you noted just now - reference doesn’t extend lifetime of a value
that make sense :-)
now I’m calm :-)
@jplatte thanks, you are as usual have all answers :-)
Jonas Platte
@jplatte
Nov 23 2016 18:57
Your code basically means the same as
let player = {
    let tmp: A = A::new();
    let tmpRef: &mut A = tmp.play(vec![]);
    tmpRef
};
println!("{:?}", player);
The equivalent C++ code would cause a segfault in the output because you'd be using a reference to a value that has been destroyed
You're welcome! :D
Pavel Meledin
@btbvoy
Nov 23 2016 18:58
yeah, that is what rust’s selling feature is :-D
safety
@jplatte btw, just wondering. If you don’t mind could you share what is your background / past experience with C/C++, asm ? Since I noticed you are pretty quickly shooting issues which I can explain as deep knowledge of similar area (I mean more low level programming, which is closer to hardware)
Jonas Platte
@jplatte
Nov 23 2016 19:01
Oh god don't mention asm and C :D
Pavel Meledin
@btbvoy
Nov 23 2016 19:01
:-D
Jonas Platte
@jplatte
Nov 23 2016 19:01
Yeah the language I am most proficient in is definitely C++
I guess I know a few quirks C has that C++ doesn't as well, but I very much avoid ever using it (with Rust existing now, I also avoid C++ for personal projects though)
And asm I only have conceptual knowledge of, I never did anything real with it
Pavel Meledin
@btbvoy
Nov 23 2016 19:03
:-D I see.
Jonas Platte
@jplatte
Nov 23 2016 19:04
Also I did some Haskell over the last 2 - 2.5 years, although I don't think I've seen any question about / have talked about type theory related things here yet.
@btbvoy What background do you come from?
Pavel Meledin
@btbvoy
Nov 23 2016 19:06
I had around 10 year in PHP that’s the most solid then I switched to Scala for almost 3 years already. Then I started discovering Erlang for my personal project and after that I switched to explorer Rust
Jonas Platte
@jplatte
Nov 23 2016 19:10
Hm, so all garbage-collected languages? Yeah I can imagine that for people like you the Rust learning curve might be relatively steep ^^
Pavel Meledin
@btbvoy
Nov 23 2016 19:11
GC is exactly why I’m here :-D
I also interested in feature that utilize hardware more effective
plus IoT direction is also interested for me
Jonas Platte
@jplatte
Nov 23 2016 19:13
Heh, so I guess you want exactly the features I have in C++ anyway.
Pavel Meledin
@btbvoy
Nov 23 2016 19:14
I was looking into C++ world
specifically for linux development / embedded systems
but that was not that easy way to start :-D
Jonas Platte
@jplatte
Nov 23 2016 19:15
I actually came to Rust just because I am curious about new languages, and stayed because of the many FP features it has while still being intuitive wrt. resource usage. And of course the great community :D
Pavel Meledin
@btbvoy
Nov 23 2016 19:16
yeah. I also found it awesome because of FP features and resource utilization