Why there is no downcast method
⚓ Rust 📅 2026-04-17 👤 surdeus 👁️ 2use std::{any::Any, collections::HashMap};
fn main() {
println!("Hello, world!");
let s = SHM::new();
s.get(&Screen::Login).unwrap().downcast_ref::<Login>();
}
trait Page: Any {}
struct Login;
impl Page for Login {}
type SHM = HashMap<Screen, Box<dyn Page>>;
#[derive(Debug, Hash, PartialEq, Eq)]
enum Screen {
Login,
}
Checking tdowncast v0.1.0 (E:\Coding\Code\Rust\tdowncast)
error[E0599]: no method named `downcast_ref` found for reference `&Box<dyn Page>` in the current scope
--> src\main.rs:6:36
|
6 | s.get(&Screen::Login).unwrap().downcast_ref::<Login>();
| ^^^^^^^^^^^^
|
help: there is a method `as_ref` with a similar name
|
6 - s.get(&Screen::Login).unwrap().downcast_ref::<Login>();
6 + s.get(&Screen::Login).unwrap().as_ref::<Login>();
|
For more information about this error, try `rustc --explain E0599`.
3 posts - 3 participants
🏷️ Rust_feed