I am a beginner with Rust facing this problem.
I need to get string value and return it.
Problem is that the value is out of scope outside the loop.
If I try to use variable that is defined in scope I cant change its value.
Starange thing is that it ir readable inside the loop (in scope) but not mutable.
On the below I have tried to find sollution byt tryin diffrent variables, but without success!
How can I get the value (here filen) to return?
fn tempstring() -> (String,String)
{
let mut contents = String::new();
let filen2 = String::new();
let mut filen = "28-123456789012".to_string();
for pathi in fs::read_dir("/sys/devices/w1_bus_master1").unwrap()
{
let file = pathi.unwrap().path();
if file.file_name().unwrap().to_str().unwrap().starts_with("28-")
{
let filen2 = format!("{}",file.to_str().unwrap());
let filen1 = file.file_name().unwrap().to_str().unwrap();
println!("{}", filen);
let mut filen = filen1.to_string().shrink_to_fit();
println!("{}", filen2);
let strfile = format!("{}{}",file.to_str().unwrap(),"/w1_slave");
let mut fil = File::open(strfile).expect("Ei löydy!");
fil.read_to_string(&mut contents).expect("vika");
}
}
println!("f2:{}", filen2);
return (filen,contents)
}
It compiles ok.
Warnings about that wariable see the gargo output below:
warning: unused variable: filen
--> src/main.rs:25:11
|
25 | let mut filen = filen1.to_string().shrink_to_fit();
| ^^^^^ help: if this is intentional, prefix it with an underscore: _filen
|
= note: #[warn(unused_variables)] on by default
warning: variable does not need to be mutable
--> src/main.rs:16:5
|
16 | let mut filen = "28-123456789012".to_string();
| ----^^^^^
| |
| help: remove this mut
|
= note: #[warn(unused_mut)] on by default
warning: variable does not need to be mutable
--> src/main.rs:25:7
|
25 | let mut filen = filen1.to_string().shrink_to_fit();
| ----^^^^^
| |
| help: remove this mut
warning: rusting (bin "rusting") generated 3 warnings
Finished dev [unoptimized + debuginfo] target(s) in 0.11s
and the output of run (the main() that displays the return values is not in my previous post):
28-123456789012
/sys/devices/w1_bus_master1/28-3c01d6074aff
f2:
28-123456789012
/sys/devices/w1_bus_master1/28-3c01d6074aff
f2:
Anturin 28-123456789012 Lämpötila on 20 astetta
Thanks for your hint!
Somehow I understood that you always use let to assing a value to variable.
(old basic method confused).
When tried without found the sollution.
fn tempstring() -> (String,String)
{
let mut contents = String::new();
let mut filen = String::new();
//let mut filen = "28-123456789012".to_string();
for pathi in fs::read_dir("/sys/devices/w1_bus_master1").unwrap()
{
let file = pathi.unwrap().path();
if file.file_name().unwrap().to_str().unwrap().starts_with("28-")
{
filen = format!("{}",file.file_name().unwrap().to_str().unwrap());
println!("filen inside is : {}", filen);
let strfile = format!("{}{}",file.to_str().unwrap(),"/w1_slave");
let mut fil = File::open(strfile).expect("Ei löydy!");
fil.read_to_string(&mut contents).expect("vika");
}
}
println!("filen outside is :{}", filen);
return (filen,contents)
}