These are chat archives for rust-lang/rust

18th
Sep 2018
Michal 'vorner' Vaner
@vorner
Sep 18 2018 11:23
Hello. Today I've hit something called type_length_limit. What exactly is that, did I do something wrong and what does the number I can set it to mean?
Andrey Ivanov
@a-nigredo
Sep 18 2018 12:53
Hi all, I have small program here https://github.com/a-nigredo/info/blob/master/src/main.rs but it does not compile with an error
`matches` does not live long enough --> src/main.rs:51:25 | 44 | rt::run(mdo! { | _____________- 45 | | let matches = App::new("Github user repo viewer") 46 | | .version("1.0") 47 | | .author("Andrii Ivanov <a.nigredo@gmail.com>") ... | 51 | | let user_name = matches.value_of("user-agent").unwrap(); | | ^^^^^^^ borrowed value does not live long enough ... | 72 | | }) 73 | | }.map_err(|err| { | |_____- borrowed value only lives until here | = note: borrowed value must be valid for the static lifetime....
Can somebody point me out what is wrong?
Lyle Mantooth
@IslandUsurper
Sep 18 2018 13:38
At a guess, matches.value_of() returns a reference. The easiest thing is probably to put add clone: let user_name = matches.value_of("user-agent").unwrap().clone();.
Honestly, all the macro stuff from mdo is making it hard to reason through the scopes.
Andrey Ivanov
@a-nigredo
Sep 18 2018 13:41
Ok, let me reimplemented via and_then chaining
Lyle Mantooth
@IslandUsurper
Sep 18 2018 13:47
Also, I don't grok futures at all, so I probably can't help as much as either of us would like.
Andrey Ivanov
@a-nigredo
Sep 18 2018 13:57
Lyle Mantooth
@IslandUsurper
Sep 18 2018 14:09
@a-nigredo, still the same error, basically?
Andrey Ivanov
@a-nigredo
Sep 18 2018 14:09
yes
Lyle Mantooth
@IslandUsurper
Sep 18 2018 14:14
Yeah. If I remember how move closures work, user_name contains a reference to matches, but it gets moved into the closure on line 60. But matches doesn't get moved, so it gets dropped when the main thread is done . . . I'm not actually sure that makes sense and is the real reason. But the solution should be either to stop user_name from being a reference so it doesn't matter when matches is dropped, or move matches into the closure with user_name.
Since user_name is a &str, I'd start with adding .to_owned() and taking a reference to it whenever you need to later.
Andrey Ivanov
@a-nigredo
Sep 18 2018 14:16
@IslandUsurper thanks, I'll try it
@IslandUsurper to_owned() works, big thanks
Lyle Mantooth
@IslandUsurper
Sep 18 2018 14:20
Great!
D'Silva
@evnix
Sep 18 2018 19:54
fn returnHello() { return "Hello" }
what is thr right return type for that function?
Karrq
@LandKingdom
Sep 18 2018 19:55
&str
D'Silva
@evnix
Sep 18 2018 19:56
it doesn't compile: &str { ^ expected lifetime parameter
Karrq
@LandKingdom
Sep 18 2018 19:56
I guess you don't know what a lifetime is. In that case I encourage you to look it up, but here's the return type with lifetime specified: &'statis str
sorry: &'static str
D'Silva
@evnix
Sep 18 2018 19:57
ah it worked!!
Karrq
@LandKingdom
Sep 18 2018 19:57
that works because you hardcoded that string in
D'Silva
@evnix
Sep 18 2018 19:58
Thanks, seems like I have a lot to learn. I finally got my HelloWorld!! :)
Karrq
@LandKingdom
Sep 18 2018 19:58
a static lifetime means it is valid for the entirety of the program
no problem, I still need to learn! I'm currently reading The Book (aka The Rust Programming Language), the 2018 Edition. I'm almost done, clocking in at chapter 18
It does a very thorough job at explaining Rust concepts, so it's a recommended read for people wanting to delve in Rust for the first time :)
D'Silva
@evnix
Sep 18 2018 20:02
Thanks, I'll go through the book. I am planning to convert one of my project written in Go to Rust.
Karrq
@LandKingdom
Sep 18 2018 20:03
Neat! I would like to know why you chose to convert it, and why Rust, if you can share
D'Silva
@evnix
Sep 18 2018 20:10
The initial prototype was written in Erlang, but then I found it too difficult to work with. I ended up spending time to create many libraries from scratch. The community is small with little documentation. Then I moved to Go which pretty much fit in right away, it had everything that I pretty much wished for, but I couldn't get the memory working as expected. Most of the times the running program would be a memory hog(likely the GC) and then there were various ways that one could introduce leaks(Lot of cases where the GC wouldn't help). Rust does everything Go does and I kind of feel safe while programming in it, so giving it a try
Karrq
@LandKingdom
Sep 18 2018 20:15
Well for starters, but you probably know this already, Rust doesn't have a GC. It determines the scope of the variable (how long should the variable be accessible) at compile time, and frees the memory at the end of the variable's scope
D'Silva
@evnix
Sep 18 2018 20:19
Yes, I am wary of languages where I have to manage pointers and memory because of my lack of skills with memory management and other lower level stuff but looks like Rust will handle the things that I would likely miss.
Karrq
@LandKingdom
Sep 18 2018 20:21
Rust won't handle for you anything, it won't straight up compile! You need to handle it yourself, one way or the other. Most of the stuff the compiler can tell apart and infer from the code, but some stuff (like that 'static lifetime) it can't really know
D'Silva
@evnix
Sep 18 2018 20:22
Rust won't handle for you anything, it won't straight up compile!
Exactly what I would want. I really am not skilled enough to handle segmentation fault errors.
Karrq
@LandKingdom
Sep 18 2018 20:23
But you'll never need to manage pointers by yourself (cough C/C++ cough), unless you really need to fiddle with them ("unsafe {}")