I'm learning Rust, coming from Go. I'm still trying to wrap my head around some basic concepts, so apologies for what's probably a stupid question!
I want to 1. Connect to a MySQL instance and 2.) Make a query, then 3.) Parse result into Vec<Struct>
I don't understand why I can't do stuff in main
without Rust complaining.
Code sample:
use mysql::{Pool};
use std::error::Error;
use mysql::prelude::Queryable;
extern crate rawsql;
extern crate mysql;
fn main() -> Result<(), Box<dyn Error>> {
let pool = Pool::new("mysql://root:mysql@127.0.0.1:33061/ebdb")?;
let mut conn = pool.get_conn()?;
let orgs = conn
.query_map(
"SELECT customer_id, amount, account_name from payment",
|(id, name, country)| {
Organization { id, name, country }
},
)?;
// How do I print the orgs here?
}
#[derive(Debug, PartialEq, Eq)]
pub struct Organization {
pub id: Option<i32>,
pub name: String,
pub country: String
}
This results in
--> src/main.rs:8:14
|
8 | fn main() -> Result<(), Box<dyn Error>> {
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
|
= note: expected enum `std::result::Result<(), Box<(dyn StdError + 'static)>>`
found unit type `()`
I'm just testing out the language and there's something here I don't get. What do I "return" from fn main
if I want to.. do stuff in fn main
without the compiler complaining?
How do I iterate through the result of Vec<Organization>
at the last line?