Given that I know ParentId and ChildId, how would I find the UserId if the hashmap is:
HashMap<ParentId, HashMap<ChildId, HashMap<UserId, Foobar>>>
As my knowledge about Rust is pretty basic, I found that the following works but it's quite verbose, as I don't know any better:
match foobar.get(&pid) {
Some(data1) => {
println!("Found 1: {:?}", data1);
match data1.get(&cid) {
Some(data2) => {
println!("Found 2: {:?}", data2);
...and so on
},
_ => println!("Not found")
}
},
_ => println!("Not found")
}
I've also attempted chained get but it's tricky and did not find how to do it correctly:
foobar
.get(pid)?
.get(cid)?
.get(to_find)
What can I try next?
You can use Option::and_then to chain operations that return Option<_>:
let _: Option<&Foobar> = foobar.get(&pid).and_then(|map| map.get(&cid)).and_then(|map| map.get(&to_find));
Example:
use std::collections::HashMap;
fn main() {
let map: HashMap<i32, HashMap<bool, HashMap<String, bool>>> = HashMap::new();
let _: Option<&bool> = map
.get(&123)
.and_then(|map| map.get(&true))
.and_then(|map| map.get("foo"));
}
Playground
Your try with ? is also correct but it'll only work in a function that returns an Option as it returns None from the function the expression is in if any value is None, which is probably the error you're getting.
fn get(map: &HashMap<i32, HashMap<bool, HashMap<String, bool>>>) -> Option<bool> {
map.get(&123)?.get(&true)?.get("foo").cloned()
}
Edit: As #Jmb pointed out in a comment below, another option is to create and immediately call a closure so you can use the ? operator which could be more readable in certain cases:
let _: Option<&bool> = (|| map.get(&123)?.get(&true)?.get("foo"))();
Related
I am very new to Rust and decided my first program to be a brainfuck interpreter.
I plan on using jump tables as the solution for the loops.
However I decided to rewrite the method to make it look better (for my tastes) and i got an error that I can't quite understand why
Code before causes no errors:
fn process_jumps(jump_map: &mut Vec<usize>, instructions: &Vec<Inst>){
let mut stack: Vec<usize> = Vec::new();
for (i, inst) in instructions.iter().enumerate() {
match inst {
Inst::LoopOpen => stack.push(i),
Inst::LoopClose => {
jump_map[i] = stack.pop();
jump_map[jump_map[i]] = i;
}
_ => ()
}
}
}
Code after has an error (marked in code):
fn process_jumps(instructions: &Vec<Inst>) -> Vec<usize> {
let mut jump_table: Vec<usize> = Vec::new();
let mut stack: Vec<usize> = Vec::new();
for (i, inst) in instructions.iter().enumerate() {
match inst {
Inst::LoopOpen => stack.push(i),
Inst::LoopClose => {
jump_table[i] = stack.pop(); // expected `usize`, found `Option<usize>`
jump_table[jump_map[i]] = i;
}
_ => ()
}
}
return jump_table;
}
My main question is why my code before didn't need me to check the optional?
Vec's pop() method returns Option<T>, not T.
You need to get the usize value from inside that Option, just make sure you've handled the None case correctly. When you are sure None is not possible, the simplest thing you could do is to unwrap() it.
Neither of your examples should really compile, as they both try to assign Option<usize> to a Vec<usize>.
In this below example (here is the Rust playground), I need to filter values in an iterator that match a specific pattern.
One way I found is to use a match returning an Option in filter_map:
#[derive(Copy, Clone, Debug)]
struct Entity(i32);
enum Event {
EntityInserted(Entity),
EntityRemoved(Entity),
}
fn main() {
let [entity1, entity2] = [Entity(1), Entity(2)];
let events = vec![
Event::EntityInserted(entity1),
Event::EntityRemoved(entity1),
Event::EntityInserted(entity2),
];
let inserted_entities: Vec<_> = events
.iter()
.filter_map(|event| match event { // <---
Event::EntityInserted(entity) => Some(entity), // <--- Those lines
_ => None, // <--- here
}) // <---
.collect();
dbg!(inserted_entities);
}
Is there a more idiomatic way to have this behavior ?
As for the filter_map, I think that's the way to go.
Curiously, I wrote a macro yesterday that generates a Some in case the if was "successful", and otherwise evaluates to None:
macro_rules! if_then_some {
($cond: expr, $val: expr) => {
if $cond { Some($val) } else { None }
};
(let $pattern:pat = $expr: expr, $val: expr) => {
if let $pattern = $expr { Some($val) } else { None }
};
}
As it stands, it can be used for checking bool-conditions and if let-conditions. In your case it could be used like this:
let inserted_entities: Vec<_> = events
.iter()
.filter_map(|event|
if_then_some!(let Event::EntityInserted(entity)=event, entity)
)
.collect();
It is disputable if this is "idiomatic", but imho it is terse yet quite readable.
A bit off-topic: On the other hand, whenever I see if let with something else than Option or Result, I am wary that the compiler does not warn me if I add new variants that should be checked in these conditions.
The following code example is the best that I have come up with so far:
enum Variant {
VariantA(u64),
VariantB(f64),
}
fn main() {
let my_vec = vec![Variant::VariantA(1),
Variant::VariantB(-2.0),
Variant::VariantA(4),
Variant::VariantA(3),
Variant::VariantA(2),
Variant::VariantB(1.0)];
let my_u64_vec = my_vec
.into_iter()
.filter_map(|el| match el {
Variant::VariantA(inner) => Some(inner),
_ => None,
})
.collect::<Vec<u64>>();
println!("my_u64_vec = {:?}", my_u64_vec);
}
I would like to know if there is a less verbose way of obtaining the vector of inner values (i.e., Vec<u64> in the example). It feels like I might be able to use something like try_from or try_into to make this less verbose, but I cannot quite get there.
Enums are not "special" and don't have much if any implicitly associated magic, so by default yes you need a full match -- or at least an if let e.g.
if let Variant::VariantA(inner) = el { Some(inner) } else { None }
However nothing prevents you from implementing whatever utility methods you're thinking of on your enum e.g. get_a which would return an Option<A> (similar to Result::ok and Result::err), or indeed to implement TryFrom on it:
use std::convert::{TryFrom, TryInto};
enum Variant {
VariantA(u64),
VariantB(f64),
}
impl TryFrom<Variant> for u64 {
type Error = ();
fn try_from(value: Variant) -> Result<Self, Self::Error> {
if let Variant::VariantA(v) = value { Ok(v) } else { Err(()) }
}
}
fn main() {
let my_vec = vec![Variant::VariantA(1),
Variant::VariantB(-2.0),
Variant::VariantA(4),
Variant::VariantA(3),
Variant::VariantA(2),
Variant::VariantB(1.0)];
let my_u64_vec = my_vec
.into_iter()
.filter_map(|el| el.try_into().ok())
.collect::<Vec<u64>>();
println!("my_u64_vec = {:?}", my_u64_vec);
}
I have working example of a simple loop (mostly taken from the odbc crate's example):
use std::io;
use odbc::*;
use odbc_safe::AutocommitOn;
fn main(){
let env = create_environment_v3().map_err(|e| e.unwrap()).unwrap();
let conn = env.connect_with_connection_string(CONN_STRING).unwrap();
let mut stmt = Statement::with_parent(&conn).unwrap();
loop {
let mut sql_text = String::new();
println!("Please enter SQL statement string: ");
io::stdin().read_line(&mut sql_text).unwrap();
stmt = match stmt.exec_direct(&sql_text).unwrap() {
Data(mut stmt) => {
let cols = stmt.num_result_cols().unwrap();
while let Some(mut cursor) = stmt.fetch().unwrap() {
for i in 1..(cols + 1) {
match cursor.get_data::<&str>(i as u16).unwrap() {
Some(val) => print!(" {}", val),
None => print!(" NULL"),
}
}
println!();
}
stmt.close_cursor().unwrap()
}
NoData(stmt) => {println!("Query executed, no data returned"); stmt}
}
}
}
I don't want to create new Statements for each query, as I just can .close_cursor().
I'd like to extract the loop's body to a function, like this:
fn exec_stmt(stmt: Statement<Allocated, NoResult, AutocommitOn>) {
//loop's body here
}
But I just can't! The .exec_direct() method mutably consumes my Statement and returns another. I tried different ways to pass Statement arg to the function (borrow, RefCell, etc), but they all fail when using in a loop. I am still new to Rust, so most likely I just don't know something, or does the .exec_direct's Statement consumption makes it impossible?
There's no nice way to move and then move back values through parameters. It's probably best to copy what .exec_direct does and just make the return type of your function a statement as well.
The usage would then look like this:
let mut stmt = Statement::with_parent(&conn).unwrap();
loop {
stmt = exec_stmt(stmnt);
}
and your function signature would be:
fn exec_stmt(stmt: Statement<...>) -> Statement<...> {
match stmt.exec_direct() {
...
}
}
I probably wouldn't recommend this, but if you really wanted to get it to work you could use Option and the .take() method.
fn exec_stmt(some_stmt: &mut Option<Statement<...>>) {
let stmt = some_stmt.take().unwrap();
// do stuff ...
some_stmt.replace(stmt);
}
The odbc-safe crate tried to have each state transition of ODBC reflected in a different type. The odbc-api crate also tries to protect you from errors, but is a bit more subtle about it. Your use case would be covered by the the Preallocated struct.
The analog example from the odbc-api documentation looks like this:
use odbc_api::{Connection, Error};
use std::io::{self, stdin, Read};
fn interactive(conn: &Connection) -> io::Result<()>{
let mut statement = conn.preallocate().unwrap();
let mut query = String::new();
stdin().read_line(&mut query)?;
while !query.is_empty() {
match statement.execute(&query, ()) {
Err(e) => println!("{}", e),
Ok(None) => println!("No results set generated."),
Ok(Some(cursor)) => {
// ...print cursor contents...
},
}
stdin().read_line(&mut query)?;
}
Ok(())
}
This will allow you to declare a function without any trouble:
use odbc_api::Preallocated;
fn exec_statement(stmt: &mut Preallocated) {
// loops body here
}
I am confused about the Some(T) keyword.
I want to check for two variables, if the value is defined (not None). If that is the case, the value of this variables is processed.
I know the match pattern which works like this:
match value {
Some(val) => println!("{}", val),
None => return false,
}
If I use this pattern, it will get very messy:
match param {
Some(par) => {
match value {
Some(val) => {
//process
},
None => return false,
}
},
None => return false,
}
This can't be the right solution.
The is a possibility, to ask if the param and value is_some() That would effect code like that:
if param.is_some() && value.is_some() {
//process
}
But if I do it like that, I always have to unwrap param and value to access the values.
I thought about something like this to avoid that. But this code does not work:
if param == Some(par) && value == Some(val) {
//process
}
The idea is that the values are accessible by par and val like they are in the match version.
Is there any solution to do something like this?
If I have several Option values to match, I match on a tuple of the values:
enum Color {
Red,
Blue,
Green,
}
fn foo(a: Option<Color>, b: Option<i32>) {
match (a, b) {
(Some(Color::Blue), Some(n)) if n > 10 => println!("Blue large number"),
(Some(Color::Red), _) => println!("Red number"),
_ => (),
}
}
fn main() {
foo(Some(Color::Blue), None);
foo(Some(Color::Blue), Some(20));
}
This allows me to match the combinations that are interesting, and discard the rest (or return false, if that is what you want to do).
If your function is processing multiple Option values, and would like to discard them if they're not Some, your function could return an Option itself:
fn foo(param: Option<usize>, value: Option<usize>) -> Option<usize> {
let result = param? + value?;
Some(result)
}
This will short-circuit the function in case there's a None value stored in either param or value.
Please read the book for more information on the ? operator.
If your function can't return an Option, you can still get away with destructuring using if let or match:
let x = if let (Some(p), Some(v)) = (param, value) {
p + v
} else {
return 0;
}
let x = match (param, value) {
(Some(p), Some(v)) => p + v,
(Some(p), _) => p,
(_, Some(v) => v,
_ => return 0,
}
Please read What is this question mark operator about? for more information on the ? operator
Please read this chapter in Rust by Example for more information on destructuring multiple things at once
There's a couple more alternatives not yet listed:
If you're willing to use experimental features (and hence the nightly compiler) you can use a try block as an alternative of extracting a function.
#![feature(try_blocks)]
fn main() {
let par: Option<f32> = Some(1.0f32);
let value: Option<f32> = Some(2.0f32);
let x: Option<f32> = try { par? + value? };
println!("{:?}", x);
}
Another alternative is to use map which only applies if the value is not None
let x: Option<f32> = par.map(|p| value.map(|v| p + v));