Rust Random number use rand::Rng; not implement error my VS Code

use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();

    let n1: u8 = rng.gen();
    let n2: u16 = rng.gen();
    println!("Random u8: {}", n1);
    println!("Random u16: {}", n2);
    println!("Random u32: {}", rng.gen::<u32>());
    println!("Random i32: {}", rng.gen::<i32>());
    println!("Random float: {}", rng.gen::<f64>());



Random u8: 205
Random u16: 55524
Random u32: 1496389568
Random i32: 857590649
Random float: 0.8847720290119313


   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.72s
     Running `target/debug/playground`

This is Correct Not Error


My VS Code Error Why?

You need to add the rand crate to the [dependencies] section of your Cargo.toml file like rand = "0.8".


You can also automatically add the dependency to your Cargo.toml using the command cargo add rand (this will also find out the latest version number for you, and use that version number by default).


I'm added cargo add rand.
But Not Working :smiling_face_with_tear:

Also Error not fixing

My Toml File.

why the code that worked in playground doesn't work for me in my VS Code. Why can't implement libraries?
I don't understand.

I'm Added but not working :melting_face:

I think the issue is that you have a main function that's not in

I took it to the file and it didn't work.

Otherwise, I open another rust file and work on it. runs when I give cargo run. I run another file with ALT + CTRL + N or through Code Runner.

Yes. In particular, the file must contain the main function for the crate that is being built (in this case rust-bro).
If you want additional binaries, then you must make a bin folder under src. There you can define other files such as Those files must also contain a main function each and can be built by cargo.
So, in your case, you need to move the file to a src/bin. Then you can run it with cargo run --bin generate-random-values.
Please don't use Code Runner to run Rust files/projects, it causes more confusion than convenience.

Why don't use Code Runner.
Isn't it more convenient to use Code Runner?

I don't know how to run a file other than so I was using Code Runner.

help me please :smiling_face_with_tear:

Judging by the fact that you appear ti have binary files in your project source directory, you might be running rustc directly to compile your code. That won't work as soon as you have dependencies (or rely on syntax not supported in Rust 2015 edition), for that reason it's generally recommended to never invoke rustc yourself and always use cargo. If that isn't how you are running your code, you need to tell us how you actually do it. Maybe start by trying to run the code via the terminal (cargo run). Once that works, you can work on how to also make it run using whatever vscode extension you are using. Generally rust-analyzer is a good option to use.

I could easily solve such errors in TypeScript or C#.

Because with CodeRunner you cannot use any dependencies.

In Rust, it is more typical to run "projects" instead of files. In your case, the project, which is called a "crate" (in Rust) has only one file.
But its okay. You can use cargo run to run the binary defined by and cargo run --bin generate-random-values to run the binary generated by (provided you place this file in src/bin).

Not working

Asking for help repeatedly, complaining, or comparing to other programming languages won't help you or us with your problem. Instead, describing more accurately all the relevant information of what it is you are doing to produce error messages and working less with hard to interpret screenshots, or one-liner responses that leave it completely open to interpretation or guesswork as to what it is you actually tried, is something that would help.

Asking for technical support online is an acquired skill, I suppose, but please at least note that asking for technical support online is something where, as far as I can tell, you still have room for improvement, and it isn't the fault of Rust, or a lack of people to help you that's causing difficulty.

I agree with your words, thank you very much, I really don't have a friend who knows Rust to help me, so learning online is more difficult.

1 Like

Packages with multiple binary crates (i.e. the feature that the --bin flag is used for if I remember correctly) are somewhat more advanced usage of cargo. (I wouldn’t know myself how to do that off the top of my head, instead I would need to look that up in the cargo book probably.) For a better experience, I would recommend to limit yourself to crates with a single main function in the file, and to create multiple independent crates in separate directories (crated e. g. with cargo new) if you want to work on multiple independent crates to play around with.

If you are familiar with git, and don't want too many independent folders just for experimenting with Rust in your machine, creating multiple branches in a single repository can also be a reasonable approach.

I'm to try cargo new file single main rust for random numbers.

Yes, that’s the easiest approach I believe. Navigate out of the rust-bro directory, then execute something like cargo new generate-random-values, then cd generate-random-values and cargo add rand; then copy the code currently in into the file in the newly created directory, and run it with cargo run. That should make everything work.

As a subsequent step, once you know that you have your code set up in a way that a plain cargo run (without additional arguments) is able to run it, you could work on your IDE experience, so for example you could try to set up e.g. the rust-analyzer extension for vscode, open the generate-random-values directory in vscode and see if checking and running the code via the IDE works fine, too. Rust-analyzer should also display additional information inside of the code, give type information and documentations when hovering over items, offer auto completion and useful quick-fixes, etc.

1 Like

just moment