Hello
Sorry for the late reply. So I'm a bit confused. I used this from the rust book and it worked. But when I modify the code so that the main function doesn't return anything it stops working. Could somebody tell me what's happening? Really appreciate all the help.
Code without main function's return value changed -
use rand_distr::{Distribution, Normal, NormalError};
use rand::thread_rng;
use ndarray::{Array};
fn main() -> Result<(), NormalError> {
let mut rng = thread_rng();
let normal = Normal::new(0., 1.0)?;
let a = Array::from_shape_fn((5, 5, 5), |_| normal.sample(&mut rng));
println!("{:?}", a);
Ok(())
}
Code with the main function's return type removed -
use rand_distr::{Distribution, Normal, NormalError};
use rand::thread_rng;
use ndarray::{Array};
fn main() {
let mut rng = thread_rng();
let normal = Normal::new(0., 1.0)?;
let a = Array::from_shape_fn((5, 5, 5), |_| normal.sample(&mut rng));
println!("{:?}", a);
}
Corresponding error -
cocoafedora@fedora:~/Documents/Code/RustLearning/random_arrays$ cargo run
Compiling random_arrays v0.1.0 (/home/cocoafedora/Documents/Code/RustLearning/random_arrays)
warning: unused import: `NormalError`
--> src/main.rs:1:40
|
1 | use rand_distr::{Distribution, Normal, NormalError};
| ^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/main.rs:7:38
|
5 | / fn main() {
6 | | let mut rng = thread_rng();
7 | | let normal = Normal::new(0., 1.0)?;
| | ^ cannot use the `?` operator in a function that returns `()`
8 | |
... |
11 | | println!("{:?}", a);
12 | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Result<Infallible, NormalError>>` is not implemented for `()`
error[E0283]: type annotations needed for `rand_distr::Normal<{float}>`
--> src/main.rs:7:18
|
7 | let normal = Normal::new(0., 1.0)?;
| ------ ^^^^^^^^^^^ cannot infer type for type `{float}`
| |
| consider giving `normal` the explicit type `rand_distr::Normal<{float}>`, where the type parameter `{float}` is specified
|
= note: multiple `impl`s satisfying `{float}: Float` found in the `num_traits` crate:
- impl Float for f32;
- impl Float for f64;
note: required by a bound in `rand_distr::Normal::<F>::new`
--> /home/cocoafedora/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_distr-0.4.3/src/normal.rs:147:10
|
147 | where F: Float, StandardNormal: Distribution<F>
| ^^^^^ required by this bound in `rand_distr::Normal::<F>::new`
error[E0283]: type annotations needed for `rand_distr::Normal<{float}>`
--> src/main.rs:7:18
|
7 | let normal = Normal::new(0., 1.0)?;
| ------ ^^^^^^^^^^^ cannot infer type for type `{float}`
| |
| consider giving `normal` the explicit type `rand_distr::Normal<{float}>`, where the type parameter `{float}` is specified
|
= note: multiple `impl`s satisfying `StandardNormal: Distribution<{float}>` found in the `rand_distr` crate:
- impl Distribution<f32> for StandardNormal;
- impl Distribution<f64> for StandardNormal;
note: required by a bound in `rand_distr::Normal::<F>::new`
--> /home/cocoafedora/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_distr-0.4.3/src/normal.rs:147:33
|
147 | where F: Float, StandardNormal: Distribution<F>
| ^^^^^^^^^^^^^^^ required by this bound in `rand_distr::Normal::<F>::new`
error[E0283]: type annotations needed for `rand_distr::Normal<{float}>`
--> src/main.rs:7:18
|
7 | let normal = Normal::new(0., 1.0)?;
| ------ ^^^^^^ cannot infer type for type `{float}`
| |
| consider giving `normal` the explicit type `rand_distr::Normal<{float}>`, where the type parameter `{float}` is specified
|
= note: multiple `impl`s satisfying `{float}: Float` found in the `num_traits` crate:
- impl Float for f32;
- impl Float for f64;
note: required by a bound in `rand_distr::Normal`
--> /home/cocoafedora/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_distr-0.4.3/src/normal.rs:118:10
|
118 | where F: Float, StandardNormal: Distribution<F>
| ^^^^^ required by this bound in `rand_distr::Normal`
help: consider specifying the type argument in the function call
|
7 | let normal = Normal::<F>::new(0., 1.0)?;
| +++++
error[E0283]: type annotations needed for `rand_distr::Normal<{float}>`
--> src/main.rs:7:18
|
7 | let normal = Normal::new(0., 1.0)?;
| ------ ^^^^^^ cannot infer type for type `{float}`
| |
| consider giving `normal` the explicit type `rand_distr::Normal<{float}>`, where the type parameter `{float}` is specified
|
= note: multiple `impl`s satisfying `StandardNormal: Distribution<{float}>` found in the `rand_distr` crate:
- impl Distribution<f32> for StandardNormal;
- impl Distribution<f64> for StandardNormal;
note: required by a bound in `rand_distr::Normal`
--> /home/cocoafedora/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_distr-0.4.3/src/normal.rs:118:33
|
118 | where F: Float, StandardNormal: Distribution<F>
| ^^^^^^^^^^^^^^^ required by this bound in `rand_distr::Normal`
help: consider specifying the type argument in the function call
|
7 | let normal = Normal::<F>::new(0., 1.0)?;
| +++++
error[E0283]: type annotations needed for `rand_distr::Normal<{float}>`
--> src/main.rs:7:18
|
7 | let normal = Normal::new(0., 1.0)?;
| ------ ^^^^^^^^^^^^^^^^^^^^ cannot infer type for type `{float}`
| |
| consider giving `normal` the explicit type `rand_distr::Normal<{float}>`, where the type parameter `{float}` is specified
|
= note: multiple `impl`s satisfying `{float}: Float` found in the `num_traits` crate:
- impl Float for f32;
- impl Float for f64;
note: required by a bound in `rand_distr::Normal`
--> /home/cocoafedora/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_distr-0.4.3/src/normal.rs:118:10
|
118 | where F: Float, StandardNormal: Distribution<F>
| ^^^^^ required by this bound in `rand_distr::Normal`
help: consider specifying the type argument in the function call
|
7 | let normal = Normal::new(0., 1.0)::<F>?;
| +++++
error[E0283]: type annotations needed for `rand_distr::Normal<{float}>`
--> src/main.rs:7:18
|
7 | let normal = Normal::new(0., 1.0)?;
| ------ ^^^^^^^^^^^^^^^^^^^^ cannot infer type for type `{float}`
| |
| consider giving `normal` the explicit type `rand_distr::Normal<{float}>`, where the type parameter `{float}` is specified
|
= note: multiple `impl`s satisfying `StandardNormal: Distribution<{float}>` found in the `rand_distr` crate:
- impl Distribution<f32> for StandardNormal;
- impl Distribution<f64> for StandardNormal;
note: required by a bound in `rand_distr::Normal`
--> /home/cocoafedora/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_distr-0.4.3/src/normal.rs:118:33
|
118 | where F: Float, StandardNormal: Distribution<F>
| ^^^^^^^^^^^^^^^ required by this bound in `rand_distr::Normal`
help: consider specifying the type argument in the function call
|
7 | let normal = Normal::new(0., 1.0)::<F>?;
| +++++
Some errors have detailed explanations: E0277, E0283.
For more information about an error, try `rustc --explain E0277`.
warning: `random_arrays` (bin "random_arrays") generated 1 warning
error: could not compile `random_arrays` due to 7 previous errors; 1 warning emitted