0

I'm new to Rust and I'm trying to open a window with the Conrod library, like they did in the canvas.rs example:

#[macro_use] extern crate conrod;
extern crate find_folder;
extern crate piston_window;

use conrod::{Canvas, Theme, Widget, color};
use piston_window::{EventLoop, OpenGL, PistonWindow, UpdateEvent,     WindowSettings};

fn main() {
    const WIDTH: u32 = 800;
    const HEIGHT: u32 = 600;

    // Change this to OpenGL::V2_1 if not working.
    let opengl = OpenGL::V3_2;

    // Construct the window.
    let mut window: PistonWindow =
    WindowSettings::new("Canvas Demo", [WIDTH, HEIGHT].opengl(opengl).exit_on_esc(true).vsync(true).build().unwrap();
   window.set_ups(60);
}

This code works when I use it in a a file in the Conrod project (the one I downloaded from GitHub), but it does not work when I use it in my own code:

extern crate conrod;
extern crate piston_window;

fn main() {
    println!("Hello, world!");
}

With the following Cargo.toml:

[package]
name = "hello_conrod"
version = "0.1.0"
authors = ["omega"]

[dependencies]
conrod = "0.37.2"

Then the compiler tells me this:

error: can't find crate for `piston_window` [E0463]

I guess my Cargo.toml is wrong but I don't have a clue what I should do.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Omegaspard
  • 1,828
  • 2
  • 24
  • 52

1 Answers1

2

You need the piston_window crate from crates.io. Just add this to your Cargo.toml, under dependencies:

piston_window = "0.51.1"

Whenever you see extern crate _, you will need to add the crate in your Cargo.toml file. The documentation on crates.io shows the different ways of importing crates (locally, optionally, from Git, etc.)

Aurora0001
  • 13,139
  • 5
  • 50
  • 53
  • In the past i tried to add the crate piston_window by adding the pinston_window = "*" to my cargo.toml but it did not work, but i'm still trying what you just told me, the code is compiling.. – Omegaspard Jul 31 '16 at 16:34
  • It's working ty, but i don't anderstand, isn't that suppose to work i had piston_window = "*" in the toml ? And how am i suppose to know the last version of the dependecy i'm adding ? – Omegaspard Jul 31 '16 at 16:45
  • 1
    When you search for a package on crates.io, there is a box with the declaration to copy, for example on the [lazy_static](https://crates.io/crates/lazy_static) page, you can see the line `lazy_static = "0.2.1"`. Don't worry - no-one expects you to learn and remember the versions, but it's bad practice to use "*" because it means your code could break in the future. – Aurora0001 Jul 31 '16 at 16:47