Returning real and imaginary parts of complex numbers

I'm very new to the Rust programming language and I am excited to get going. I have been using Julia for several months now to process complex numbers. However, I want to transfer some of my algorithms to a compiled language. Rust may be over kill because I could use C, but I want to try something new and Rust looks very nice. I wrote the following little snippet and it runs. However, I need to extract the real and imaginary parts and I have been unsuccessful in finding functions in the "num" crate to do that. I'm looking for something like fn real() and fn imag(), but I do not see such functions. What am I missing? Thanks for any help!

//Example of simple RUST program using while loop

extern crate num;

use num::complex::Complex;

fn main() {

let mut n=0.0;

let x = Complex::new(-1.0, 0.0);

while n < 1.01 {

	let y = Complex::powf(&x, n);

	println!("n = {:0.2}, y ={:0.4}",n,y);




looking at the docs for complex numbers, it is a struct with named fields re and im, so you can do to get the real part, and to get the imaginary part.

Please format your code with code blocks like this:

// your code here

Anyway, the Complex type in num is defined like this

pub struct Complex<T> {
    pub re: T,
    pub im: T,

so you can simply access the re and im fields as they are public.


Wow! Thank you for your fast and helpful response.

Thanks Alice for both advising regarding the code blocks format and the answer to my question. Warmest regards.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.