Hello,
I'm seeking advice regarding the actual code writing in rust. Please assume an experienced programmer who learns rust, coming from the C++/Java worlds.
While understanding rust's philosophy and core concepts does not seem like an issue, the struggle comes with practical scenarios like the one described below.
Please bear with me I sound silly, but perhaps we're doing it wrong? Perhaps these are bugs/issues to be solved and I should report them?
That said - issues like these hamper our productivity and irritate newcomers and so are a concern.
I'm looking for any kind of advice.
OK, so imagine I need to generate a random integer between x and y. I could google for that, but it just so happens I can ask colleague Joe about it, and he says:
"Hey, we're using this standard rand
crate. Take thread rng or something from it and you're good"
That advice sounds plausible and so I type (vs code with rust-analyzer):
let mut rng = rand::
and the IDE obliges with random()
and thread_rng()
completions. (NOTE: I'm not allowed to include more than 1 media item in a post, so no screenshot here...)
OK, great! There's this thread_rng
that Joe was talking about. So I end up with:
let mut rng = rand::thread_rng();
Now, let's see what can I do with my rng
:
(again, no screenshot due to the media item limit, but the available completions to let x = rng.
are:
clone()
clone_from()
clone_into()
into()
to_owned()
try_into()
Ooops... this doesn't look promising. There doesn't seem to be anything in the popup that looks related to random number generation.
Joe is not here, so I take a look at the docs:
All right... there are 4 interesting functions there:
fn next_u32(&mut self) -> u32
fn next_u64(&mut self) -> u64
fn fill_bytes(&mut self, dest: &mut [u8])
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
Joe is back, so I bug him again:
"Hey Joe, this ThreadRng
looks pretty low-level. Ain't there some API to generate integers in range? I don't feel like using next_u32() % something
"
"What do you mean? There are these gen*
methods in ThreadRng
"
Oh, are there?
Let me try... I type:
Which yields more results... Interesting...
And I'm totally baffled when I type rng.gen
which shows 4 completions (sorry, can't attach another screenshot):
gen()
gen_bool()
gen_range()
gen_ratio()
Eventually, I'm able to locate the get_range
function and use it, but this is not a great experience and relying on somebody else's expertise for trivial things won't scale.
What is going on?
Is it a rust-analyzer bug that some methods are hidden from completion (it's not like there are gazillions of them...)? Is it perhaps a configuration issue? Any way I could help to debug or improve it?
Why wasn't I able to find these gen*
methods in the docs?
There may be perfectly good answers to these questions, but the current state seems to move us through more hardship than needed or expected during the transition towards rust
Please help
FWIW, VS Code version:
Version: 1.73.1
Commit: 6261075646f055b99068d3688932416f2346dd3b
Date: 2022-11-09T03:54:53.913Z
Electron: 19.0.17
Chromium: 102.0.5005.167
Node.js: 16.14.2
V8: 10.2.154.15-electron.0
OS: Linux x64 5.15.0-52-generic snap
Sandboxed: No
Tested rust-analyzer v0.3.1285 and v0.4.1284