Hi!
First post from a new Rust developer. I've taken some code I had in Python and previously ported to Go to give myself a real project. The code requires the AWS SDK (rusoto), HTTP (reqwest) and JSON (serde_json).
I saw lots of examples while working through this where a question mark was used at the end of a line, the function declared as returning Result<()> and it returned the value Ok(()). There's a few examples of this pattern at serde_json - Rust
I couldn't get this to compile, so I wrote the smallest program I could:
fn main() -> Result<()> {
println!("Hello, world!");
Ok(())
}
When compiling, I see:
▶ cargo run
Compiling result v0.1.0 (/Users/thebazzer/rust/result)
error[E0107]: this enum takes 2 type arguments but only 1 type argument was supplied
--> src/main.rs:1:14
|
1 | fn main() -> Result<()> {
| ^^^^^^ -- supplied 1 type argument
| |
| expected 2 type arguments
|
note: enum defined here, with 2 type parameters: `T`, `E`
--> /Users/thebazzer/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:241:10
|
241 | pub enum Result<T, E> {
| ^^^^^^ - -
help: add missing type argument
|
1 | fn main() -> Result<(), E> {
| ^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0107`.
error: could not compile `result`
To learn more, run the command again with --verbose.
If I try to fix the problem as suggested:
fn main() -> Result<(), E> {
println!("Hello, world!");
Ok(())
}
Then I see:
▶ cargo run
Compiling result v0.1.0 (/Users/thebazzer/rust/result)
error[E0412]: cannot find type `E` in this scope
--> src/main.rs:1:25
|
1 | fn main() -> Result<(), E> {
| ^ help: a trait with a similar name exists: `Eq`
|
::: /Users/thebazzer/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/cmp.rs:268:1
|
268 | pub trait Eq: PartialEq<Self> {
| ----------------------------- similarly named trait `Eq` defined here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0412`.
error: could not compile `result`
To learn more, run the command again with --verbose.
Frankly, I'm somewhat baffled. In the end, I removed the question marks, and used match to process the return value, which did work:
let sitr:Result<SigninTokenBody> = serde_json::from_str(&*body);
match sitr {
Ok( json ) => {
let sign_in_url = get_signin_url( json.signin_token );
println!( "{}", sign_in_url );
},
Err( err) => println!( "JSON Error: {}", err ),
}
If anyone could shed any light on this, I would appreciate it!
Thanks!