1

So I have a main.rs file that looks like so:

fn main() {
    yew::Renderer::<App>::new().render();
}

But I need to import the App component into main.rs. From what I saw in a tutorial I should be able to do: use <name_of_app>::App;, but that does not work. It will tell me that <name_of_app> is not a part of the crate for me to use and Rust analyzer is not coming up with suggestions.

I tried this approach, but Rust analyzer is complaining that App is being defined multiple times:

mod App;
use App::App;

fn main() {
    yew::Renderer::<App>::new().render();
}
Daniel
  • 14,004
  • 16
  • 96
  • 156
  • @kmdreko, yes but I did not want to name my file `lib.rs`, so I named it `App.rs`. Can I not get away with that? As a ReactJS developer I am trying to organize the project the way I would ordinarily do if it was a React project. – Daniel Dec 14 '22 at 23:39
  • I deleted by comment about `lib.rs`, but you may not be aware that `main.rs` and `lib.rs` are *special* files in a cargo project. So renaming it did more than you think. – kmdreko Dec 14 '22 at 23:41
  • @kmdreko, you are right, I was not aware of that. – Daniel Dec 14 '22 at 23:42
  • 1
    *"Rust analyzer is complaining that `App` is being defined multiple times"* - ha, that's funny. You should probably follow convention and use lowercase file names (`app.rs` and `mod app;`, `use app::App;`) to avoid this problem. – kmdreko Dec 14 '22 at 23:43
  • @kmdreko, using Rust convention of a lowercase `app.rs` worked. Making note of convention. Thank you. – Daniel Dec 14 '22 at 23:46
  • 1
    Or you can get rid of the `use App::App;` line and type `App::App` everywhere, e.g. `yew::Renderer::::new().render();` – Jmb Dec 15 '22 at 08:05

2 Answers2

1

I encountered the same problem, but I solved it in a different way, I hope you can refer to the following.

I have wrapped the render method of the App component inside the component file,like app.rs

#[function_component(App)]
fn app() -> Html {
  // some code
}

pub fn render() {
    yew::Renderer::<App>::new().render();
}

then in main.rs, you can use app mod and call app::render()

mod app;

fn main() {
    app::render();
}
Maxwell
  • 11
  • 1
0

You'll need to define a module for whatever file your App is defined in. mod name_of_app; use name_of_app::App;

src/main.rs

mod module_for_app;
use module_for_app::App;

fn main() {
    yew::Renderer::<App>::new().render();
}

src/module_for_app.rs

pub struct App {
   // ....
}

See the section from The Book on modules for more information

PitaJ
  • 12,969
  • 6
  • 36
  • 55
  • my App function is defined in the App file. `mod App::App` is not going to go down well with Yew. Can you suggest a better naming convention in the world of Rust? I am a React developer so I am really approaching this file organization in a ReactJS way. – Daniel Dec 14 '22 at 23:36
  • @Daniel you did not read the answer correctly. If your `App` is in `App.rs` then you need *both* `mod App;` (to declare it) and `use App::App;` (to import it) – kmdreko Dec 14 '22 at 23:40
  • @kmdreko, check out my original post, I edited it, perhaps than can clarify things and help you, help me. – Daniel Dec 14 '22 at 23:42