6

I'm fairly new to rust and have been following the official book that they provide on their site. During chapter 2 they tell you to import a "Rand" cargo which I did. However, when I try to run my code directly through VS Code I get an error saying "unresolved import rand". When I run it through command prompt, everything works fine. I've already tried every solution suggested here: https://github.com/rust-lang/rls-vscode/issues/513 and nothing seemed to have worked. Extensions that I'm using:

  • Better TOML
  • Cargo
  • Code Runner
  • Rust (rls)
  • Rust Assist
  • vsc-rustfmt
  • vscode-rust-syntax

Has anyone else ran into a similar problem or a know a solution? Thank you!

Edit: My Cargo.TOML looks like this:

[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Name <MyNameHere@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.6.0"

Edit 2: my main.rs file looks like this:

use rand::Rng;
use std::io;
use std::cmp::Ordering;

fn main() {
    println!("Guess the number!");
    let secret_number = rand::thread_rng().gen_range(1, 101);
    loop {
        println!("Please input your guess!");
        let mut guess = String::new();
        io::stdin().read_line(&mut guess).expect("Failed to read line!");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };
        println!("Your guess {}", guess);
        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}
Mark Moretto
  • 2,344
  • 2
  • 15
  • 21
KapitaiN
  • 373
  • 1
  • 4
  • 14
  • What are the contents of `Cargo.toml`? – E_net4 Feb 20 '20 at 11:58
  • @E_net4theharmedSOmember eddited the question – KapitaiN Feb 20 '20 at 12:06
  • What about the actual import? Can you show us the code? – E_net4 Feb 20 '20 at 12:10
  • 1
    @E_net4theharmedSOmember added it as well – KapitaiN Feb 20 '20 at 12:14
  • 2
    Is your VS Code workspace the same folder where you have the `Cargo.toml` (the root one)? Because if it's a parent folder it won't work. If you have nested projects maybe you'll have issues to detect installed dependencies in multiple `Cargo.toml` files in sub projects. – Genarito Feb 20 '20 at 16:39
  • @Genarito yes my workspace is in the correct folder. Interesting thing is that all seems fine in IntelliJ when I use the IntelliRust plugin. Though I still want to use VS code for this – KapitaiN Feb 21 '20 at 17:51

1 Answers1

8

Got a fix!

In VSC, select Extensions, select the Code Runner extension, click the little gear symbol and select Extension Settings. It's the Code-runner: Executor Map setting that needs to be changed. Click the 'Edit in settings.json' link.

Add the following to the file:

"code-runner.executorMap": {
   "rust": "cargo run # $fileName"
}

If you already have content in the settings.json file then remember to add a comma to the line above and put your edit inside the outermost curly braces, e.g.

{
    "breadcrumbs.enabled": true,
    "code-runner.clearPreviousOutput": true,
    "code-runner.executorMap": {
        "rust": "cargo run # $fileName"
    }
}

This tells Code Runner to use the 'cargo run' command, instead of 'rustc'

This fix came from this question on stackoverflow.

Wendelin
  • 2,354
  • 1
  • 11
  • 28
Jaff
  • 104
  • 2