How to add a new external crate:
https://doc.rust-lang.org/cargo/guide/dependencies.html
The second function, Items
Since the second function ('fn section_item') doesn't contain much new compared with the first ('fn section_header') I think won't need many comments. There's one thing though...
To stop the program from exiting after each function, I call 'main()' at the very end. This statement is now added to the first function also.
There's a risk here: the first instance of the script calling the next and so on. I haven't fully checked that yet, I haven't made a "cargo release" yet. We'll see later.
The second function looks like this:
fn item(section_item:String,fname_ini:String){
println!("Item was: {}",section_item);
let f = open_file (fname_ini.to_string());
// getting the section item & item value
let mut prompt:String = "Section item chosen. Please enter the item name: >>".to_string();
let inp_name:String =rdl(prompt).trim().to_string();
let mut prompt:String = "Section item chosen. Please enter the item text: ";
let inp_value:String =rdl(prompt).trim().to_string();
// putting together the complete item line
// constructing the output
let mut inp_line = String::new();
inp_line.push_str(&inp_name.as_str()); // text from console, item name
let delim:String=String::from(": ");
inp_line.push_str(&delim); // add delimiter
inp_line.push_str(&inp_value); // text from console, item value
let n:String = String::from("\n"); // let's add a NewLine (needing 2 statements!)
inp_line = inp_line + &n;
println!("Complete item line is: {} ",inp_line);
// adding the item line to the file
let res = write_file(inp_line.to_string(), f);
let res = match res {
Ok(res) => res,
Err(error) => error.to_string(),
}
main();
}
Console example running the whole application:
Enter something: [[
I didn't expect that to work! [[
Header was: [[
Section header chosen. Please enter the header text: >> test1
Complete header is:
[test1]
Enter something: [
Item was: [
Section item chosen. Please enter the item name: >>item3
Section item chosen. Please enter the item name text: >>whatever
Complete item line is: item3: whatever
File ed.ini contents:
___________ // space here
[test]
item0: foo
item1: bar
[test1]
item3: whatever
----------- // end here
This will read the .ini file and print the lines, numbered. It will make it easier to do rudimentary
editing later. The green statements are the new things I've not touched on earlier, pointing to contents
by dereferencing ('*') and creating an .iter of the vector and then iterating over it ("for val in...").
I've added a couple of lines before the statement "let input=rdl("Enter something: ".to_string());"
in main(), to show a little help:
[[=header, [=item, /*=start comment, */=end comment, l=list file
Enter something: l
1 [test]
2 item0: foo
3 item1: bar
4
5 [test1]
6 item3: whatever
[[=header|[=item|/*=start comment|*/=end comment|l=list file
Enter something: ^C
snkdb@pf-ZBook-15:~/rust/projects/mutability_moving_borrowing/src$
Inga kommentarer:
Skicka en kommentar