These are chat archives for rust-lang/rust

15th
Apr 2018
Adam M Ludvik
@aludvik
Apr 15 2018 01:41
If you want to modify a value pointed to by a reference I believe you need to dereference it first with *.
so something like |x| { *x << 1 }
3442853561
@3442853561
Apr 15 2018 04:01
Hello everyone, how could I compile Rust to JavaScript or asm.js? there is a platform (WeChat minigame) can't support wasm
Paul Reesman
@reesmanp
Apr 15 2018 04:34
@aludvik thanks for that. I tried the dereference earlier and it didn't work. I just tried it again and added a ; then it worked like a charm.
Karan Chaudhary
@lafolle
Apr 15 2018 13:17

Hi,
I'm getting this error on trying to understand an error:

lafolle@yoss:~/rustlang/rhi$ cargo --explain E0477
error: no extended information for E0477

error: process didn't exit successfully: `rustc --explain E0477` (exit code: 101)
lafolle@yoss:~/rustlang/rhi$

Am i missing something?

using this: rustc 1.25.0 (84203cac6 2018-03-25)
Josh
@joshlemer
Apr 15 2018 15:46
Hey I was wondering, why is it that std::io::Lazy does not appear in the rustdocs even though it is in the stdlib?
Adam M Ludvik
@aludvik
Apr 15 2018 16:07
@joshlemer I think the problem is that the module lazywhich it is contained in isn’t pub: https://github.com/rust-lang/rust/blob/master/src/libstd/io/mod.rs#L306
Josh
@joshlemer
Apr 15 2018 16:08
oh ok that means that it's not usable to the public right?
that's too bad! Lazy vals are pretty nice :-)
maybe in a future release
Adam M Ludvik
@aludvik
Apr 15 2018 16:13
Yeah that’s my understanding. You can control access at the module, struct, and function levels. Another example of where this is useful is it allows you to include tests in the same file as the thing they are testing: https://github.com/rust-lang/rust/blob/master/src/libstd/io/mod.rs#L2162 You wouldn’t want your docs full of test modules or your users depending on your tests.
Josh
@joshlemer
Apr 15 2018 22:28
Hey I was wondering, how can I annotate the type of this closure? The following doesn't compile:
let nums = vec![1, 2, 3];

let takes_nums: fn() -> Vec<i32> = || nums; // doesn't compile
let takes_nums2: fn() -> Vec<i32> = move || nums; // doesn't compile either
let takes_nums3: fn() -> Vec<i32> = || vec![1,2,3]; // does compile
Bryan Burgers
@bryanburgers
Apr 15 2018 22:45
What does the compiler say when it doesn’t compile?
Josh
@joshlemer
Apr 15 2018 22:50
error[E0308]: mismatched types
  --> src/main.rs:16:40
   |
16 |     let takes_nums: fn() -> Vec<i32> = move || nums;
   |                                        ^^^^^^^^^^^^ expected fn pointer, found closure
   |
   = note: expected type `fn() -> std::vec::Vec<i32>`
              found type `[closure@src/main.rs:16:40: 16:52 nums:_]
Adam M Ludvik
@aludvik
Apr 15 2018 22:51
I believe fn() is just for “regular” functions and Fn(), FnOnce(), and FnMut() are for closures. See this for more info: https://doc.rust-lang.org/std/ops/trait.Fn.html
This trait (Fn) is not to be confused with function pointers (fn).
Josh
@joshlemer
Apr 15 2018 22:53
yeah but isn't it wierd that this compiles then?
let takes_nums3: fn() -> Vec<i32> = || vec![1,2,3]; // does compile
Bryan Burgers
@bryanburgers
Apr 15 2018 22:55
What are you trying to do with take_nums? If you don't give it a type, but pass it to something that expects an FnOnce() -> Vec<i32>, it works.
Adam M Ludvik
@aludvik
Apr 15 2018 22:58
Since takes_nums3 isn’t actually capturing any variables, maybe the compiler is just treating the third closure as plain function?
Josh
@joshlemer
Apr 15 2018 22:59

Oh I'm just trying to learn how it works. I just wanted to see what is the actual type of it, since it actually behaves differently than other functions, because it can only be called once:

take_nums();
take_nums(); // error

But also I was trying to figure out how to capture nums in a closure, and use it multiple times. I am surprised that I can't do this by using the word move

Josh
@joshlemer
Apr 15 2018 23:04

I had tried this approach:

    let takes_nums = {
        let nums = vec![1, 2, 3];
        move || &nums
    };
    takes_nums();
    takes_nums();

but the lifetimes don't work for some reason

Adam M Ludvik
@aludvik
Apr 15 2018 23:06
Adding move to the closure causes any captured variables to be moved out of the containing scope when the closure is created. In your example, take_nums would be referencing a variable that was moved into the closure take_nums2.
Josh
@joshlemer
Apr 15 2018 23:08
yes I'd like for it to capture nums
Bryan Burgers
@bryanburgers
Apr 15 2018 23:09
The way I understand (not sure if it’s right) is that if the compiler allowed you to call take_nums twice, it would mean that two things owned nums.
Josh
@joshlemer
Apr 15 2018 23:09
I think it seems like, take_nums should be of type fn() -> &'lifetime_of_take_nums Vec<i32>
Bryan Burgers
@bryanburgers
Apr 15 2018 23:09
And since two things can’t own nums, the compiler doesn’t allow you to do that.
Josh
@joshlemer
Apr 15 2018 23:09
but not sure how to refer to the lifetime of take_nums

if the compiler allowed you to call take_nums twice, it would mean that two things owned nums

Yes that's exactly right I think. However, in this case I'm trying to just give out a immutable reference

Josh
@joshlemer
Apr 15 2018 23:33
@aludvik thanks for the sample I'll try and understand this