Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Execute a simple SQL report using Rust and Diesel
extern crate diesel;
use diesel::prelude::*;
table! {
users (id) {
id -> Int4,
first_name -> Nullable<Varchar>,
last_name -> Nullable<Varchar>,
pub struct User {
pub id: i32,
pub first_name: Option<String>,
pub last_name: Option<String>,
fn main() {
let database_url = "postgres://pat@localhost:5432/names";
let connection = PgConnection::establish(&database_url).expect(
&format!("Error connecting to {}", database_url)
use users::dsl::*;
let results = users.load::<User>(&connection).expect(
"Error loading users"
let (mult, one): (Vec<User>, Vec<User>) =
if let Some(ref name) = user.last_name {
name.split(' ').collect::<Vec<&str>>().len() > 1
} else {
println!("Found {} users with more than one last name.", mult.len());
println!("Found {} users with one last name.", one.len());

This comment has been minimized.

Copy link

ccouzens commented Jun 20, 2018

 name.split(' ').collect::<Vec<&str>>().len() > 1

Could you use the count method here?

 name.split(' ').count() > 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.