-1

I have a structure and a function call:

pub fn get_exchanges(database: &str) -> Result<Vec<Exchanges>> {
    todo!()
}

pub struct Exchanges {
    pub rowid: u64,
    pub table_name: String,
    pub profile_id: String,
    pub name: String,
    pub etl_type: i16,
    pub connection_info: String,
    pub update_flag: bool,
    pub date_last_update: String,
    pub gui_show: bool,
    pub reports_flag: bool,
}

I want to pull out the name within the structure and do some work on it. I have an iterator but cannot figure out how to get to the specific item called name.

let exchanges_list = get_exchanges("crypto.db");
for name in exchanges_list {
    println!("exchange {:?}", name);
    //ui.horizontal(|ui| ui.label(exchange));
}

The result is

 Exchanges { rowid: 4, table_name: "exchanges", profile_id: "None", name: "coinbasepro_noFlag", etl_type: 2, connection_info: "{\"name\":\"coinbase_pro\",\"base_url\":\"https://api.pro.coinbase.com\",\"key\":\"[redacted]\",\"secret\":\"[redacted]\",\"passphrase\":\"[redacted]\"}", update_flag: false, date_last_update: "2009-01-02 00:00:00 UTC", gui_show: true, reports_flag: true }
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
John
  • 1
  • 3
  • I’d suggest you might want to remove the secret, passphrase, and key info if those are as confidential as their names sound, since this is a public site that probably gets trawled for such things. – Lux Mar 08 '22 at 20:53
  • 3
    I have removed them and requested a moderator expunge from the history. OP, you should still immediately invalidate those credentials and request new ones. The Internet never forgets... – cdhowie Mar 08 '22 at 20:57
  • [Why does `Option` support `IntoIterator`?](https://stackoverflow.com/q/43285372/155423) – Shepmaster Mar 08 '22 at 21:05

2 Answers2

1

get_exchanges returns a Result, which represents either success or failure. This type implements IntoIterator and is therefore a valid value to be iterated in a for loop, but in this case that's just iterating over the single Vec value. If you want to iterate the Vec itself then you need to unwrap the Result. The standard way to do this is using the ? operator, which will propagate any error out of your function and unwrap the Result into the contained value:

let exchanges_list = get_exchanges("crypto.db")?;
//                           Add this operator ^

If your function does not return a compatible Result type, you can instead use get_exchanges("crypto.db").unwrap(), which will panic and crash the whole program at runtime if the Result represents a failure. This may be an acceptable alternative if your program doesn't have anything useful to do when encountering an error at this point.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
0

First, you need to call .unwrap() since the value is a Result; that’ll let you use the Vec inside. Then, you can iterate with .iter() if you want to get the names:

let exch_lst = get_exchanges("crypto.db").unwrap();
for exch in exch_lst.iter() {
    println(“exchange: {}”, exch.name.as_str());
}

There are other methods besides for + iter; this is just the one that seemed to fit best with what you already had.

Lux
  • 1,540
  • 1
  • 22
  • 28