Use of undeclared crate or module

I'm trying to use the lib.rs code from this WebAssembly example in a binary crate in the same package:

https://rustwasm.github.io/wasm-bindgen/examples/2d-canvas.html

However, I'm getting the "use of undeclared crate or module".

error[E0432]: unresolved import `canvas`
 --> src/bin/bin.rs:1:5
  |
1 | use canvas::start;
  |     ^^^^^^ use of undeclared crate or module `canvas`

canvas/src/lib.rs:

use std::f64;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(start)]
pub fn start() {
    ...
    ... Code can be found here:
    ... https://rustwasm.github.io/wasm-bindgen/examples/2d-canvas.html
    ...
}

canvas/src/bin/bin.rs:

use canvas::start;

fn main() {
	start()
}

canvas/Cargo.toml:

[package]
name = "canvas"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
js-sys = "0.3.69"
wasm-bindgen = "0.2.92"

[dependencies.web-sys]
version = "0.3.4"
features = [
  'CanvasRenderingContext2d',
  'Document',
  'Element',
  'HtmlCanvasElement',
  'Window',
]

Change your [lib] section in Cargo.toml to this:

[lib]
crate-type = ["cdylib", "rlib"]

This will build a Rust library (“rlib”) for use by rustc and a cdylib for linking with external tools.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.