It's perhaps useful to read one *short* and outstandingly clear article on strings, slices and string literals:
https://blog.thoughtram.io/string-vs-str-in-rust/
Highly recommended. That article should be a part of the rust documentation!
When I say "Let's run it" below, remember that what I'm running is the whole program code
OK, back on track. In the last blog post it was time to write the results from the comment_start() function. Here I was bound to get in trouble, my PHP experience tricking me into un-Rustable behavior. Here the statements for opening and writing to the file are added. Lets run it (results below the code):
fn comment_start(fname_ini:String) {
let mut s = String::new();
let a:&str = "1";
let b:&str = "1";
while a == "1" && b == "1" {
s = t_get(s);
let n1 = s.len()-1;
let ch1 = s.chars().nth(n1).unwrap();
print!("{}",ch1);
io::stdout().flush().unwrap();
s=t_get(s);
let n2 = s.len()-1;
let ch2 = s.chars().nth(n2).unwrap();
print!("{}",ch2);
io::stdout().flush().unwrap();
if (ch1 == '*') && (ch2 == '/') {
print!("\n\n{}",s);
let f = open_file( fname_ini);
write_file(s, f);
list(fname_ini);
io::stdout().flush().unwrap();
exit(0);
}
if ch2 == '*' {
s=t_get(s);
let n3 = s.len()-1;
let ch3 = s.chars().nth(n3).unwrap();
print!("{}",ch3);
io::stdout().flush().unwrap();
if ch3 == '/' {
let f = open_file(fname_ini);
write_file(s, f);
list(fname_ini);
exit(0);
}
}
}
}
Compiling playground v0.1.0 (/home/snkdb/rust/projects/playground)
--> src/main.rs:182:14
|
160 | fn comment_start(fname_ini:String) {
| --------- move occurs because `fname_ini` has type `std::string::String`, which does not implement the `Copy` trait
...
180 | let f = open_file( fname_ini);
| --------- value moved here
181 | write_file(s,f);
182 | list(fname_ini);
| ^^^^^^^^^ value used here after move
error[E0382]: use of moved value: `fname_ini`
--> src/main.rs:196:18
|
160 | fn comment_start(fname_ini:String) {
| --------- move occurs because `fname_ini` has type `std::string::String`, which does not implement the `Copy` trait
...
194 | let mut f = open_file(fname_ini);
| --------- value moved here
195 | write_file(s,f);
196 | list(fname_ini);
| ^^^^^^^^^ value used here after move
error: aborting due to 2 previous errors; 4 warnings emitted
error[E0308]: mismatched types
--> src/main.rs:180:32
|
180 | let f = open_file( &fname_ini);
| ^^^^^^^^^^
| |
| expected struct `std::string::String`, found `&std::string::String`
| help: consider removing the borrow: `fname_ini`
error: aborting due to previous error
--> src/main.rs:180:31
|
180 | let f = open_file(fname_slice);
| ^^^^^^^^^^^
| |
| expected struct `std::string::String`, found `&str`
| help: try using a conversion method: `fname_slice.to_string()`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`.
--> src/main.rs:196:18
|
160 | fn comment_start(fname_ini:String) {
| --------- move occurs because `fname_ini` has type `std::string::String`, which does not impl
ement the `Copy` trait
...
194 | let mut f = open_file(fname_ini);
| --------- value moved here
195 | write_file(s,f);
196 | list(fname_ini);
| ^^^^^^^^^ value used here after move
error: aborting due to previous error; 4 warnings emitted
Compiling playground v0.1.0 (/home/snkdb/rust/projects/playground)
Running `/home/snkdb/rust/projects/playground/target/debug/playground`
[[=header|[=item|/*=start comment|*/=end comment|l=list file
Enter something:
Compiling playground v0.1.0 (/home/snkdb/rust/projects/playground)
Running `/home/snkdb/rust/projects/playground/target/debug/playground`
[[=header|[=item|/*=start comment|*/=end comment|l=list file
This is a comment
And this is another one
*/ // command ending the comment
This is a comment //this is an unnecessary printout of the result (three lines)
And this is another one // it's the print!("\n\n{}",s); I forgot to delete! Arggh!
1 [testing1]
2 item1: some
3
4 [testing2]
5 item2: other
6
7 This is a comment
8 And this is another one
9 */ // ... and this one shouldn't be included. I'll have to fix that!
Inga kommentarer:
Skicka en kommentar