Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/rust/downcast/examples/   (Firefox Browser Version 153.0.1©)  Datei vom 27.6.2026 mit Größe 707 B image not shown  

Quelle  simple.rs

  Sprache: Rust
 

extern crate downcast;

// we need to use downcast::Any instead of std::any::Any
use downcast::{downcast, Any};

/* Trait */

trait Animal: Any {
    fn what_am_i(&self);
}

downcast!(dyn Animal);

/* Impl */

struct Bird;

impl Animal for Bird {
    fn what_am_i(&self){
        println!("Im a bird!")
    }
}

impl Bird {
    fn wash_beak(&self) {
        println!("Beak has been washed! What a clean beak!");
    }
}

/* Main */

fn main() {
    let animal: Box<dyn Animal> = Box::new(Bird);
    animal.what_am_i();
    {
        let bird = animal.downcast_ref::<Bird>().unwrap();
        bird.wash_beak();
    }
    let bird: Box<Bird> = animal.downcast::<Bird>().ok().unwrap();
    bird.wash_beak();
}

Messung V0.5 in Prozent
C=90 H=98 G=94

¤ Dauer der Verarbeitung: 0.2 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.