Variable value readable but not mutable (scope issue)

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)
}

Reformatted code:

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);
}

Can you provide us with the exact error message? I'm not sure which value you mean (maybe someone else does, though?).

Check out this post for instructions how to format code (or other text, such as error output of the compiler).


Ah, I think you mean filen2. You redefine a different variable with the same name inside the loop.


Not sure if I completely understand your code, but maybe this example can help you:

fn main() { 
    let mut s = String::new(); 
    for i in 0..3 { 
        s = format!("{}", i); // try to add a `let` here! 
    }
    println!("s = {}", s);
}

(Playground)

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

About the filen2. It is ouly done because my debugging.
Same for filen1 too!
There is a lot of lines in the code I did to try debug only.

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)
}

@petri_is_rusting When you post questions here, please format your code.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.