You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Any guidance on navigating the documentation for a sw eng. who's learning Rust
Despite being programming for 10 years (C, VHDL, JS/TS, Go, Python) I am new to Rust and its ecosystem.
I am trying to understand how to navigate the documentation.
I am reading the Rust book and got to the following line:
let secret_number = rand::thread_rng().gen_range(1..=100);
The above line was suggested to me.
But now, my goal is the opposite. How would I now which method to use?
In other programming languages implementing a random number is quite straightforward, we create a seed and generate (usually) a floating point from [0:1).
Despite understanding that this is a massive simplification and there are many use cases and parameters while generating a random number, I am not motivated to read The Rust Rand Book (at least not now) on how to generate a random number.
While searching for random I found the Functions section with thread_rng (Retrieve the lazily-initialized thread-local random number generator, seeded by the system)
I see that it returns a struct ThreadRng. Things are going well, it matches what is proposed on the book
Now, in ThreadRng's docs, I search for any method that corresponds to random or gen* as used in the book. There is none 🤷♂️
Now, what is very confusing is that in my editor I get suggestions for different gen* methods.
* I have cargo added rand = "0.8.5" and the docs correspond to the same version.
Any help is much appreciated.
Best,
The text was updated successfully, but these errors were encountered:
Hi @ace-astro, when you are new to Rust, it can be a bit confusing to find functionality implemented in traits. In this case, Rng has the the gen_range you are looking for. Unfortunately, Rng is not listed on the ThreadRng docs page, because it is an extension trait of RngCore.
Usually, important traits are imported in the examples and mentioned in the top-level docs, I recommended looking there for traits that might implement functionality you are looking for. In Rand's case:
To get you started quickly, the easiest and highest-level way to get a random value is to use random(); alternatively you can use thread_rng(). The Rng trait provides a useful API on all RNGs, while the distributions and seq modules provide further functionality on top of RNGs.
Your IDE is also helping you here, it's telling you where the methods are coming from, see the as Rng snippets.
You can also search the docs for gen_range, it will immediately find the trait providing it.
Hello,
Despite being programming for 10 years (C, VHDL, JS/TS, Go, Python) I am new to Rust and its ecosystem.
I am trying to understand how to navigate the documentation.
I am reading the Rust book and got to the following line:
The above line was suggested to me.
But now, my goal is the opposite.
How would I now which method to use?
In other programming languages implementing a random number is quite straightforward, we create a seed and generate (usually) a floating point from [0:1).
Despite understanding that this is a massive simplification and there are many use cases and parameters while generating a random number, I am not motivated to read The Rust Rand Book (at least not now) on how to generate a random number.
So my logic is:
random
I found theFunctions
section withthread_rng
(Retrieve the lazily-initialized thread-local random number generator, seeded by the system)ThreadRng
. Things are going well, it matches what is proposed on the bookThreadRng
's docs, I search for any method that corresponds torandom
orgen*
as used in the book. There is none 🤷♂️Now, what is very confusing is that in my editor I get suggestions for different
gen*
methods.* I have
cargo add
edrand = "0.8.5"
and the docs correspond to the same version.Any help is much appreciated.
Best,
The text was updated successfully, but these errors were encountered: