These are chat archives for rust-lang/rust

9th
May 2019
Stefan538871
@Stefan538871
May 09 08:08
Hi guys, I am just getting into rust and started to read https://doc.rust-lang.org/book/ch03-03-how-functions-work.html And I just read the part with ";" that you can turn a expression into a statement. The fact that "x +1" and "x +1;" is different. I would assume that this behavior leads to error-prone code. Is it common to not use ; at the end? Can you explain me some advantages? Compared to ; => End of line/Code.
Denis Lisov
@tanriol
May 09 08:30
Yes, using expressions is common in Rust. This could be error-prone in more dynamic languages, but in Rust it's the kind of errors the compiler will tell you about.
In particular, omitting ; is typical on the last line of functions instead of explicit return: my_value instead of return my_value;
Kelly Thomas Kline
@kellytk
May 09 08:52
I made a struct with the same fields as another struct. What's the most concise syntax available to create the other struct with one of the first? Ideally without manually duplicating every field ala let newstruct = Another{foo: struct.foo, bar: struct.bar...}
Denis Lisov
@tanriol
May 09 08:58
Normally you need to manually duplicate the fields. IIRC there were crates providing derives for something like that.
Stefan538871
@Stefan538871
May 09 11:26
Thx, for the response. I just thought a little bit about shadowing. I.e. let x = 1; let x = 2;
If i understood this one correct, internal rust created a new variable (because x is immutable) and assigned it to 2. What happens to the old x? Is there a way to get the value of the old x? OR is there a garbage collector to clean up the old x?
Even though i probably think the main usage of shadowing is type conversion (i.e. integer -> double)
Denis Lisov
@tanriol
May 09 11:32
The old x still lives till the end of the function, where before returning it will be dropped just like any other local variable. Yes, if, for example, you've made a reference to the old x, it will be usable even when the variable itself is shadowed.
Kelly Thomas Kline
@kellytk
May 09 11:46
@tanriol Do you happen to recall the name of the crates?
Denis Lisov
@tanriol
May 09 11:47
@kellytk Is this part of code performance sensitive or not?
Kelly Thomas Kline
@kellytk
May 09 12:08
No
Denis Lisov
@tanriol
May 09 12:13
The only crate I remember about that should work in more or less general case is frunk
Kelly Thomas Kline
@kellytk
May 09 12:19
Thanks
matrixbot
@matrixbot
May 09 17:29
hector.lopez Ola buena tarde
chisana
@chisa0a
May 09 18:38
buenex
Robyn Speer
@rspeer
May 09 20:13
I'm trying to figure out how to make a non-cryptographic RNG, that produces uniformly distributed floating-point values, with a deterministic seed. I have tried for like an hour to find documentation of this, but what I am finding is instead documentation about how to implement PRNGs in Rust.
What I'm currently trying is: let mut rng = SmallRng::from_seed([1, 2, 3, 4]);
I got that from an example somewhere, but it told me:
no function or associated item named `from_seed` found for type `rand::rngs::small::SmallRng` in the current scope
function or associated item not found in `rand::rngs::small::SmallRng`
help: items from traits can only be used if the trait is in scope
... Okay, so including use rand::prelude::*; has made the error more helpful, although such an import makes me very nervous coming from the Python world. Is this really the right thing to do?
Denis Lisov
@tanriol
May 09 20:16
use rand::SeedableRng;
Robyn Speer
@rspeer
May 09 20:16
The new error is the first thing to tell me that SmallRng wants a sequence of 16 ints, not 4.
Documentation could have told me that, but all the rand docs are like "when you make a PRNG you get to decide what it's seeded by! Be free!"
[1, 2, 3, 4] is from an example I found on a GitHub issue, though honestly that issue was from 2014 so I understand why the solution is wrong, but in general I'd like some current documentation with examples about how to use rand.
Thanks, Denis. How should I have learned from the rustc output that I needed to use rand::SeedableRng?
Denis Lisov
@tanriol
May 09 20:20

It should have written that below :-)

2 |     rand::rngs::SmallRng::from_seed(());
  |     ----------------------^^^^^^^^^
  |     |
  |     function or associated item not found in `rand::rngs::small::SmallRng`
  |
  = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:
  |
1 | use rand_core::SeedableRng;

The exact path is different, but the hint is usually there.

Robyn Speer
@rspeer
May 09 20:21
The examples at https://docs.rs/rand/0.5.6/rand/rngs/index.html include SmallRng::from_seed, so it sure looks like SmallRng has a from_seed function.
But... you're right, Denis, apparently what I encountered was a bug in VS Code where it ate half of the rustc output.
Okay, I think I'm getting back on track. Is there documentation with examples, though?
of the rand crate?
Denis Lisov
@tanriol
May 09 20:25
There are two examples at https://github.com/rust-random/rand/tree/master/examples , not sure about more.
Robyn Speer
@rspeer
May 09 20:25
That helps, thanks a lot.
Robyn Speer
@rspeer
May 09 20:31
And of course the rand::rngs documentation I found a few minutes ago has at least that short example. But that documentation actually raises another question for me.
I think this is the current version: https://docs.rs/rand/0.6.0/rand/rngs/index.html
Under "Seeding" it says: "(Don't use StdRng or SmallRng in this case since different algorithms may be used by future versions of Rand; use an algorithm from the prng module.)"
Denis Lisov
@tanriol
May 09 20:31
The latest is for 0.6.5
Robyn Speer
@rspeer
May 09 20:31
where "prng" is a link, and when I click on it, it tells me it's deprecated.
Okay, same in 0.6.5.
Denis Lisov
@tanriol
May 09 20:33
And the "deprecated" page has a link to the documentation on PRNGs :-)
Robyn Speer
@rspeer
May 09 20:35
I'm trying to deduce from this page what it actually wants me to do in the code.
I guess it wants me to use rand_xorshift::XorShiftRng;.
Denis Lisov
@tanriol
May 09 20:36
It does not want you to do in the code. It wants you to think and choose the PRNG you'll use.
Robyn Speer
@rspeer
May 09 20:36
It really doesn't matter that much, I just wanted a non-deprecated example.
I am not doing cryptography.
Denis Lisov
@tanriol
May 09 20:38
If you don't have specific requirements, any will do.
Robyn Speer
@rspeer
May 09 20:38
I mean, I really appreciate that I can at least get quick answers here.
So thanks.
I think the documentation is pretty lacking for someone who just wants to port NumPy code that calls np.random.
Denis Lisov
@tanriol
May 09 20:39
Cryptography is not the only possible kind of requirements (for example, some Monte-Carlo simulations may be incompatible with specific generators due to their statistical properties).
Robyn Speer
@rspeer
May 09 20:40
I'm sure this is a fascinating topic to learn about, but it is not what I'm interested in. I think I've got everything I need on this particular topic, pieced together from several different pages.
Denis Lisov
@tanriol
May 09 20:41
Okay, good luck with your experiments :-)
Ichoran
@Ichoran
May 09 20:41
@rspeer - It's probably not designed with your use case in mind. But most maintainers are happy to have documentation PRs that improve how welcoming the crate is.
So if you end up with enough time to help improve it, that'd be awesome. If not, hopefully you at least have what you need to make some progress.
Incidentally, the Pcg generators are also very good.
Robyn Speer
@rspeer
May 09 20:52
I might find the time to improve it, to at least make the examples about seeding more prominent; maybe in time they'll rank higher than 5-year-old wrong answers on Stack Overflow.
And to link straight to the Rand Book instead of via a deprecation page.