Hello,
I'm really new to Rust and also new to this kind of programming, so, i'll present you the code i'm working on (a simple exercise): as i'm currently working on monitoring, I want a simple « check_ram » that simple retrieve used memory and total available memory on a GNU/Linux
system.
For that I read /proc/meminfo
and parses it with a Regex. Here is the code:
extern crate regex;
use regex::Regex;
use std::io::{self, BufReader};
use std::io::prelude::*;
use std::fs::File;
struct MemResult<'a, 'b> {
regex: &'a str,
value: Option<i32>,
unit: &'b str,
}
impl<'a, 'b> MemResult<'a, 'b> {
fn update(&mut self, value: i32, unit: &'b str) {
self.value = Some(value);
self.unit = unit;
}
fn find(&mut self, text: &'b str) {
let re = Regex::new(self.regex).unwrap();
let caps = re.captures(text);
match caps {
Some(total) => self.update(
total.at(1).unwrap().parse::<i32>().unwrap(),
total.at(2).unwrap()
),
_ => (),
}
}
}
fn main() {
let mut mem_total = MemResult {
regex: "^MemTotal:[ ]+(.*) ([a-zA-Z]+)",
value: None,
unit: "kB" };
let mut mem_free = MemResult {
regex: "^MemFree:[ ]+(.*) ([a-zA-Z]+)",
value: None,
unit: "kB" };
let f = File::open("/proc/meminfo");
let r = BufReader::new(f.unwrap());
let mut mv = vec![&mut mem_total, &mut mem_free];
for l in r.lines() {
for v in mv {
v.find(l.unwrap().as_str());
}
}
for v in mv {
match v.value {
Some(val) => println!("{0}", val),
_ => ()
}
}
}
And here are the compile errors, using Cargo:
src/main.rs:52:20: 52:30 error: borrowed value does not live long enough
src/main.rs:52 v.find(l.unwrap().as_str());
^~~~~~~~~~
note: in expansion of for loop expansion
src/main.rs:51:9: 53:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:50:5: 54:6 note: expansion site
src/main.rs:39:22: 62:2 note: reference must be valid for the block suffix following statement 0 at 39:21...
src/main.rs:39 unit: "kB" };
src/main.rs:40 let mut mem_free = MemResult {
src/main.rs:41 regex: "^MemFree:[ ]+(.*) ([a-zA-Z]+)",
src/main.rs:42 value: None,
src/main.rs:43 unit: "kB" };
src/main.rs:44
...
src/main.rs:52:13: 52:41 note: ...but borrowed value is only valid for the statement at 52:12
src/main.rs:52 v.find(l.unwrap().as_str());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of for loop expansion
src/main.rs:51:9: 53:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:50:5: 54:6 note: expansion site
src/main.rs:52:13: 52:41 help: consider using a `let` binding to increase its lifetime
src/main.rs:52 v.find(l.unwrap().as_str());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of for loop expansion
src/main.rs:51:9: 53:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:50:5: 54:6 note: expansion site
src/main.rs:51:18: 51:20 error: use of moved value: `mv` [E0382]
src/main.rs:51 for v in mv {
^~
note: in expansion of for loop expansion
src/main.rs:51:9: 53:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:50:5: 54:6 note: expansion site
note: `mv` was previously moved here because it has type `collections::vec::Vec<&mut MemResult<'_, '_>>`, which is non-copyable
src/main.rs:52:20: 52:21 error: use of moved value: `l` [E0382]
src/main.rs:52 v.find(l.unwrap().as_str());
^
note: in expansion of for loop expansion
src/main.rs:51:9: 53:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:50:5: 54:6 note: expansion site
note: `l` was previously moved here because it has type `core::result::Result<collections::string::String, std::io::error::Error>`, which is non-copyable
src/main.rs:56:14: 56:16 error: use of moved value: `mv` [E0382]
src/main.rs:56 for v in mv {
^~
note: in expansion of for loop expansion
src/main.rs:56:5: 61:6 note: expansion site
src/main.rs:51:18: 51:20 note: `mv` moved here because it has type `collections::vec::Vec<&mut MemResult<'_, '_>>`, which is non-copyable
src/main.rs:51 for v in mv {
^~
note: in expansion of for loop expansion
src/main.rs:51:9: 53:10 note: expansion site
note: in expansion of for loop expansion
src/main.rs:50:5: 54:6 note: expansion site
error: aborting due to 4 previous errors
Could not compile `check_ram`.
To learn more, run the command again with --verbose.
I tried many things, also re-re-re-re-reading the Rust book, searching the net but I think i need a bit help about borrowing and lifetimes
Also, i'm currently working with Rust 1.3.0 since Gentoo hasn't already Rust 1.4.0 in ebuilds.
Thanks for reading & help