I tried today to make a program out of GitHub - plotters-rs/plotters-piston: Plotters piston window backend example, but I couldn’t. The example itself works fine; in my program, it seems that there is a conflict around the crate piston_window (E0308, perhaps two different versions of crate ‘piston_window’ are being used?). I guess some dependencies use internally piston_window, so now there’s a conflict around the piston_window I use and the one “already used”.
What’s the general approach making examples work as normal projects?
I fixed the issue by taking the used piston_window version from Cargo.lock and putting it in Cargo.toml dependencies (in my case replacing piston_window = “0.120.0” with “0.112.0”). However, the program panics when I hover over the window with the mouse cursor (Debian 12 stable, XFCE). So the main question remains: how to correctly make a project from an example?
This is written by adapting the Cargo.toml from the repo, turning dev-dependencies into dependencies, adding the plotters-piston repo itself as an additional dependency, and removing dependencies that the example doesn't use directly.
Note that edition "2021" is not matching the original example’s context, but (unsurprisingly) it looks like the code didn’t contain any edition-sensitive syntax anyway.[1]
judging by the fact that it runs fine; I have not actually read the code ↩︎
In principle, this can be a mostly systematic process to create a new package from an example:
Delete all targets from the Cargo.toml and file tree (src/*, tests/*, benches/*) except for the example.
Move the example code from examples/examplename.rs to src/main.rs.
If the example has [[example]] target configuration in Cargo.toml, turn it into [[bin]].
In the Cargo.toml, turn all [dev-dependencies] into regular [dependencies].
Remove unused dependencies (this is hard to do automatically, but also not strictly necessary).
However, it sounds like you are not trying to create a new package but integrate the example code into an existing package. Is that right? That will be harder, but there is nothing special to examples about it. Approach it as you would any problem of getting dependency versions to line up. In particular, do you really have to downgradepiston_window to a version from 2020? It’s plausible that such old code has a bug that doesn't work with your presumably more recently updated desktop environment.
Also, as general advice, whenever you use example code, make sure you’re using an example from the same version of the library that you want to use. That’s one of the common reasons to get compilation errors when trying to adapt example code. For packages released to crates.io, you can consult the docs.rs source view to see the matching examples; on GitHub, make sure the revision you're viewing is the same as the revision in your Cargo.toml or Cargo.lock.
Given the appearance of 0.112.0 in this listing, it sounds like you’ve perhaps tried to use the crates.io-version of plotters-piston.
That’s a bit more challenging, because it doesn’t appear to match exactly with the git version.
You’d need to look into how out-of-date the crates.io version is comparatively, what changed, etc… if the plotters-piston version on crates.io is sufficiently functional, too, you could work off itsCargo.toml (and also see if something relevant changed in the example); as a starting point, one can explore the crate’s files on docs.rs, and/or in your local crates cache (assuming that you’re likely to have them already cached if you used them, somewhere in a path of the form ~/.cargo/registry/src/index.crates.io-…[HASH]…/plotters-piston-0.3.0).
Edit: Tried that out, and the program panics, in much the same way you’ve described. This doesn’t mean that the example wasn’t correctly adapted, it’s just that you originally compared the example from the git version (using git versions of plotter-piston and some dependencies) with an adapted example corresponding to the crates.io version.
Edit2: Just for fun, I copied plotters-piston-0.3.0 from my local cache into a new directory, and ran the example in the context of the crate:
using the original lockfile, the example is even more broken, displaying a grey window content (and still panicking on hover)
after cargo update, i.e. dropping the original dependency locks, it’s back to the same behavior as before (panic on hover)