0

I have code where I want to use a library but it seems like we cannot import libraries within mod?

mod A {
    pub struct Circle {
        x: f64,
        y: f64,
        radius: f64,
    }

    struct BigCircle {
        x: f64,
        y: f64,
        radius: f64,
    }

    impl Circle {
        pub fn new(x: f64, y: f64, radius: f64) -> Circle {
            Circle {
                x: x,
                y: y,
                radius: radius,
            }
        }

        pub fn area(&self) -> f64 {
            std::f64::consts::PI * (self.radius * self.radius)
        }

        pub fn grow(&self, increment: f64) -> Circle {
            // BigCircle { x: self.x, y: self.y, radius: self.radius + increment }
            Circle::new(self.x, self.y, self.radius + increment)
        }

        pub fn print(&self) {
            println!("Im a Circle");
        }
    }
}

fn main() {
    let c = A::Circle::new(0.0, 0.0, 2.0);
    println!("{}", c.area());
}

When I use this code it throws an error I because of the usage of const. if I don't put my code inside mod, everything works fine

error: failed to resolve. Use of undeclared type or module `std::f64::consts` [--explain E0433]
  --> src/main.rs:27:13
   |>
27 |>             std::f64::consts::PI * (self.radius * self.radius)
   |>             ^^^^^^^^^^^^^^^^^^^^
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Omar
  • 61
  • 1
  • 6
  • I searched in Stackoverflow but could not find an answer. can u please send me the link ? – Omar Aug 30 '16 at 17:39
  • The link is at the top of the question. You'll find a link like this on all questions marked as duplicate. – Francis Gagné Aug 30 '16 at 23:57
  • But that does not really answer my question.. – Omar Aug 31 '16 at 22:48
  • How about importing external crate ? for example if I wanna use session-types library. can I import it in a mod? – Omar Aug 31 '16 at 22:59
  • I've edited the linked answer, because although I think it *does* answer your question, perhaps you simply didn't understand it. :) Is it clearer now? Note that the linked question uses a module included from a separate file, while you use an inline module, but in Rust, there's no difference between the two. – Francis Gagné Sep 01 '16 at 00:25
  • Yeah I have solved that problem already :) Thanks – Omar Sep 01 '16 at 01:59

0 Answers0