Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation navigation #1366

Open
0xBradock opened this issue Dec 17, 2023 · 2 comments
Open

Documentation navigation #1366

0xBradock opened this issue Dec 17, 2023 · 2 comments

Comments

@0xBradock
Copy link

Hello,

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.

So my logic is:

  • Go to rand's official documentation
  • 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.

image

Any help is much appreciated.

Best,

@vks
Copy link
Collaborator

vks commented Dec 17, 2023

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.

@0xBradock
Copy link
Author

Thanks for the kind answer.
It is a little more clear now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants