« first day (1891 days earlier)      last day (1604 days later) » 

1:35 AM
I wonder if Box should include a method to construct a Box<[T]> from raw pointer
seem that Box::from_raw(std::slice::from_raw_parts_mut(ptr, len)) do it
that feel so wierd
 
 
13 hours later…
2:29 PM
hi guys
 
Howdy
 
new Rustling here. just starting out with Rust
wanted to know why it was a good idea to introduce non-lexical lifetimes ? I mean isnt it counter intuitive ?
We train in lexical scopes in other languages but why did Rust plan to break the mold here ? It could have done with out it.
 
No - actually, it helps with some situations that seemed to developers to work, but wouldn't because of lifetime rules.
Let me find an example...
 
that would be great
 
I think the best set of motivations describing it is in the RFC which proposed the change. But it boils down to this:
 
2:37 PM
@ng.newbie rust still use lexical scope but not for lifetime
 
If I declare some variable, as you note, it's live until the end of the block the let statement is in. If I create a reference, it's lifetime is necessarily constrained to that block. The issue arises if, I do something like this:
 
@Stargateur Yeah but why not. I mean I can never actually use 2 mutable references then why allow me to declare them ?
 
66
Q: What are non-lexical lifetimes?

StargateurRust has an RFC related to non-lexical lifetimes which has been approved to be implemented in the language for a long time. Recently, Rust's support of this feature has improved a lot and is considered complete. My question is: what exactly is a non-lexical lifetime?

 
@Stargateur I mean it seems to me they rewrote the compiler for nothing. And yes I have read that again and again.
 
Vent: I don't understand what is so hard about finding duplicates
 
2:39 PM
@ng.newbie trust me nobody would rewrite a compiler for nothing :p
 
@ng.newbie Did you see all the linked questions in the "A sample of real problems solved by non-lexical lifetimes" section?
 
Yes then what is the real benefit of allowing them ? @Shepmaster Yes but since I am just starting out with Rust most of the code did not make much sense.
 
let a = 36i32;
let b = &a;
read_intref(b);
add_one(a); ///Without NLL, invalid - a is borrowed by b, which is still a live borrow
 
@ng.newbie this feels pretty explanatory to me: stackoverflow.com/questions/41187296/…
n.set(n.get() + 1);
This wouldn't work before NLL
@ng.newbie Let's flip the script — can you an example of where NLL has caused you confusion?
In other words, why is this sentence wrong:
> A wonderful thing about non-lexical lifetimes is that once enabled, no one will ever think about them. It will simply become "what Rust does" and things will (hopefully) just work.
 
let a = 36i32;
let b = &a;
read_intref(b);
add_one(a); //Without NLL, invalid - a is borrowed by b, which is still a live borrow
read_intref(b); //With NLL, invalid - b's lifetime is cut short of it's lexical scope, because it's referent, a is used.
 
2:53 PM
@Zarenor Ok so just correct me if I am wrong: I cannot do read_intref(b); twice because I am mutating a after the first read_intref(b); correct ?
So once I mutate `a` the previous reference goes out of scope. Am I correct ?
 
That's the idea. I'm actually subtly wrong - I put it in the playground - and the borrow is actually extended to the next use of b, making that usage of a still invalid.
But without NLL, the first example would be invalid. Because the borrow b would live until the end of the lexical scope
 
I think this would have been simpler to demonstrate
fn example2() {
    let mut a = 36;
    let b = &a;
    *b;
    a = 42;
    *b;
}
 
@Zarenor Another thing I see you declaring a as 36i32, What is that all about ? Can you put the data type in the variable ?
 
8
Q: What does 0u8 mean in Rust?

Jack SteamI'm reading through The Rust Programming Language and have encountered this notation: 0u8. #![allow(unused_variables)] fn main() { let some_u8_value = 0u8; match some_u8_value { 1 => println!("one"), 3 => println!("three"), 5 => println!("five"), 7 => pri...

 
If you read the RFC text I linked, they progress through increasingly complicated examples - the more complicated, less contrived examples are the real motivators here - like the one Shep mentioned, where you couldn't retrieve from a map, and store to it.
 
3:01 PM
@Zarenor mine wasn't actually a map, but similar concept. Just a getter and setter
 
Yes - for all numeric literals, you can write the type you intend. It only works for numeric literals. str literals have a different syntax.
 
@Shepmaster I do not understand why n.set(n.get() + 1); would not work without NLL.

n.set is a mutable function right ? To which I am passing a value from n.get(), why won't it work ?

I am sorry to ask such a stupid question but I am baffled at how different Rust is to languages like Java and Python
i am passing a mutable reference with a value whats wrong with that. maybe i will have to go through the RFC
thanks for all your help till now guys.
 
@ng.newbie it's not a stupid question, but I'm still confused why you are asking it in the first place. I'd still like to know how/why you learned about NLL in the first place and decided to concentrate on it.
As mentioned, NLL basically allows code that was actually safe but the compiler was previously too dumb to realize it.
@ng.newbie It didn't work because there was a mutable reference and an immutable reference in the same lexical scope, and that was disallowed
> As Rust evaluates arguments left to right, that code is equivalent to this:

let arg1 = &mut n;
let arg2 = n.get() + 1;
Number::set(arg1, arg2);
 
@Shepmaster Great. I learned about NLL through absolute accident. I ended up writing code from the book with 2 mutable references but forgot to put the println, since I had NLL it actually compiled. Which is what baffled me, since the book said the code was not going to compile. What is irritating me is that Rust only stops me when I use the declared variable and not while declaring them.
 
(Well, that desugaring may not be quite right, as that exact code still fails when expanded like that)
Ah, you changed your avatar since then
or it's different on chat?
 
3:10 PM
its different in the chat.
 
But yes, there's only a compiler error when you do something that is an error.
 
so wait since the desugaring is wrong does that mean the answer is wrong? I mean thats not how NLL helps if the desugaring is inaccurate right ?
 
Having two mutable references at the same time is the error. If you don't need/use the reference any longer, it's implicitly stops being effective
 
yes then why allow us to create 2 mutable references. just dont allow that. why wait till i use it.
 
@ng.newbie well, the desugaring was not 100% accurate, but that doesn't mean that the original problem wasn't solved by NLL. Just that the original was more subtle.
@ng.newbie There's no problem with the code until you use it.
Similarly, we'd normally say that "Rust doesn't allow uninitialized variables"
but this compiles with no errors:
fn main() {
    let foo: String;
}
Because we never make use of the uninitialized variable
There is a warning about an unused variable, though :-)
 
3:17 PM
@Shepmaster So are you going to edit the answer or at least tell me what is happening under the hood, because it looks perfectly legal.
A warning is not an error though
 
@ng.newbie I've updated it with a link to the "real" description
 
3:41 PM
*mut *mut *mut ::libc::c_char
SAD
 
It's actually correct in this case, but I keep looking at it and going "that's not right"
1
Q: What is the correct way to drop a mem::forgotten array of strings sent to C via an out pointer and then passed back to Rust for deletion?

marathonI would like to return some strings to C via a Rust FFI call. I also would like to ensure they're cleaned up properly. I'm creating the strings on the Rust side and turning them into an address of an array of strings. use core::mem; use std::ffi::CString; #[no_mangle] pub extern "C" fn drop_r...

a pointer to an array of C strings
 
4:19 PM
@Shepmaster to be fair in that case it's "ok but try to avoid it"
 
@Stargateur sure. I agree with your suggestion to flip the arguments to take the length as the out pointer
Using some typedefs / type aliases would also be good
 
@Shepmaster hide pointer is more evil
 
@Stargateur I figured you'd say that. I disagree, but I know your opinion isn't unique
 
> I'd rather have three-star programming than an overabundance of typedefs. Especially typedefs of pointers. Ugh. -- wrl
 
4:53 PM
@Shepmaster @Zarenor Thanks for all your help. I guess I have got my head around some of the ways the borrow checker works but I still have a long way to go. I will be back here asking my noob questions soon enough.
 
@ng.newbie That sounds about right. The borrowchecker is probably the most new and unique aspect of Rust compared to most languages
 
I am just amazed the compiler is now generating the entire control flow graph and checking if the borrows are valid are not. makes implementing a Rust compiler a whole lot harder compared to Java or Python
 
Feels like CFGs are a pretty typical part of a compiler
> 3. Transform AST into a Control Flow Graph (Python/compile.c)
> 4. Emit bytecode based on the Control Flow Graph (Python/compile.c)
I don't find any direct reference, but I'd be pretty sure that various Java compilers also have it
 
I see. But I guess most compilers just emit the source code from the CFG, but Rust actually performs checks on it.
I mean the error checking ends at the AST level.
 
I think that most static languages make use of the CFG for error reporting
it seems to be a good way to find unreachable code, for example
now, I'm not sure if the borrow checker is CFG-based or not
 
5:09 PM
well it says so in the RFC. but I am not a compiler enthusiast so I dont know what I am talking about.
 
5:48 PM
o/
 
6:08 PM
morning!
 
 
3 hours later…
9:02 PM
Interesting. Have you folks ever heard of Megazording?
 
@E_net4isunsafe yes I call it make a lib
 
Sounds like a Mozilla-specific term
Being a "best practice" also seems debatable
 
 
2 hours later…
11:06 PM
0
Q: Is it possible to overload add operator for String type in rust?

Anil KhandeiI am learning operator overloading for my own types. like example below i am implementing the add trait from standard library for my own Point type. impl Add<Point> for Point{ type Output=Point; fn add(self, point:Point)->Self::Output{ Point{ x:self.x+...

style of indentation: the no indentation
 

« first day (1891 days earlier)      last day (1604 days later) »