[SOLVED] Trying to understand the async and the error world in Rust way

Sorry for my english but i'm having a little problem with a code of rust

the enigmatic code is this:

let mut continuar = true;
                    for node in html.find(Attr("class", "read_img").descendant(Name("a"))) {
                        // dbg!(node);
                        for imagen in node.find(Name("img")) {
                            // dbg!(imagen.attr("src").unwrap());
                            match imagen.attr("src") {
                                Some(url_imagen) => {
                                    if continuar {
                                        match bajar_imagen(
                                            url_imagen,
                                            directorio_capitulo,
                                            &cliente,
                                        )
                                        .await
                                        {
                                            Ok(_) => {}
                                            Err(e) => {
                                                if e == "fichero_pequeño" {
                                                    imprimir_amarillo(
                            "Fichero muy pequeño menor o igual 195 bytes, borrar directorio",
                        );
                                                    borrar_directorio(&directorio_capitulo);
                                                    continuar = false;
                                                } else {
                                                    imprimir_amarillo(
                                                    "Otro error al bajar imagen, borrar directorio",
                                                );
                                                    println!("Error: {}", e);
                                                    borrar_directorio_y_panic(&directorio_capitulo);
                                                }
                                            }
                                        }
                                    }
                                }
                                None => {
                                    imprimir_rojo(
                                        "No se ha podido bajar la url dentro del src del html",
                                    );
                                    borrar_directorio_y_panic(&directorio_capitulo);
                                }
                            }
                        }
                    }

This funtion is async, but i was thinking that i only have one function a time

I have put a boolean name continuar, to go through the images while i don't get an especific error if i get an ERROR is a PANIC, tomorrow i will recode and do it, with .map and .collect but now i don't understand

i can pass you the playground of all of the code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=825c28db5206e408fd4d686620f08945

Perhaps it's the late night, i know that i have to redone the code more rustician way, i'm trying to get all the concepts

but my surprise is that i'm getting images, get an error, delete the directory but the program wants to write another file in the same directory. in a paralel work i will understand it, but i was thinking that i only have one threath of execution (async but only one)

This is from the terminal the messages i have seen and don't understand WHY

[src/main.rs:303] &url_imagen = "//l.mangatown.com/store/manga/29309/006.0/compressed/d000.jpg?token=def3fd3963efb203a11f3f5f32ac60544bfbc866&ttl=1585090800"
Fichero muy pequeño menor o igual 195 bytes, borrar directorio
Se ha borrado el directorio
[src/main.rs:303] &url_imagen = "//l.mangatown.com/store/manga/29309/006.0/compressed/d001.jpg?token=9a49a03549cdbf92d8c7006f226b1b8bc581ffd0&ttl=1585090800"
Error al crear fichero, borrar directorio
Error: No such file or directory (os error 2)
No he podido borrar directorio
Error: No such file or directory (os error 2)

i know that i get an error (i believe that i put the boolean continuar to false, because i want to stop in a good way, but the programs get an internal error of bajar_imagen the next one

i'm writing in a nfs systems.... ¿?¿? but with only one thread i theory don't have any problem.

Many thanks in advance now i'm getting at least seven hours of sleep and tomorrow i will try to understand this (perhaps it's a silly mistake)

With the code written in a language I don't understand and no line numbers on the errors, it's very difficult to help you.

I confirm that i'm calling the function in this way

  for capitulo in node.find(Name("option")).take(capitulos - 1) {
                            //dbg!(capitulo);
                            match capitulo.attr("value") {
                                Some(url_pagina) => {
                                    bajar_pagina(url_pagina, directorio_capitulo, &cliente).await;
                                }
                                None => {
                                    imprimir_amarillo(
                                        "No se ha podido obtener la url de la pagina",
                                    );
                                }
                            }

And sometimes
i get

Continuar = true
ERROR that i get and put continuar to false and remove the directory
Continuar = true

the program tries to write another file in the directory and fails

i'm using a for to go through the images, and call the bajar imagen in a async way, (the method bajar_capitulo is async) before downloading the image i check continuar

it's seems that in my code i got a datarace, but i was thinking in only 1 thread of execution

in a minute i compile withouth --release and put the error in the terminals

in pseudocode is

let mut continuar = true
for (imagen in node){
    if continuar {
       match download_image_async.await{
          if ok => Nothing
          if ERR => delete_the_directory;
                         continuar = false
       }
  }
}

And seems that sometimes i get an error but the program tries to get another image, before continuar is put to false

The terminal without release tells this:

https://www.mangatown.com/manga/baka_to_boing/ Dec 25,2017
https://mangatown.com/manga/baka_to_boing/v03/c032/
[src/main.rs:243] continuar = true
[src/main.rs:244] imagen.attr("src").unwrap() = "//l.mangatown.com/store/manga/5204/03-032.0/compressed/t000.jpg?token=b2224e2984184aca67ba83119155dbc1b625c812&ttl=1585119600"
[src/main.rs:304] &url_imagen = "//l.mangatown.com/store/manga/5204/03-032.0/compressed/t000.jpg?token=b2224e2984184aca67ba83119155dbc1b625c812&ttl=1585119600"
Fichero muy pequeño menor o igual 195 bytes, borrar directorio
Se ha borrado el directorio
[src/main.rs:243] continuar = true
[src/main.rs:244] imagen.attr("src").unwrap() = "//l.mangatown.com/store/manga/5204/03-032.0/compressed/t143.jpg?token=c443c9ae4793463e10a983ed14d5c16d31689af0&ttl=1585119600"
[src/main.rs:304] &url_imagen = "//l.mangatown.com/store/manga/5204/03-032.0/compressed/t143.jpg?token=c443c9ae4793463e10a983ed14d5c16d31689af0&ttl=1585119600"
Error al crear fichero, borrar directorio
Error: No such file or directory (os error 2)
No he podido borrar directorio
Error: No such file or directory (os error 2)
thread 'main' panicked at 'explicit panic', src/main.rs:41:5
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

¿??¿it's this possible, I don't understand how i can obtain the second [src/main.rs:243] continuar = true

Just a moment perhaps it's my misundestandig about the async way

i will do it in a pseudocode

let mut continuar = true
for (imagen in node){
    if continuar {
       match download_image_async.await{
          if ok => Nothing
          if ERR => delete_the_directory;
                         continuar = false
       }
  }
}

If i have the await function, how many threads i have i understand that only one

Because the problem is perhaps i have one function awaiting to write the file (is async ) and rust continue to do work.

The problem perhaps is Rust starts to download the next image, the first file crash, delete the directory, and the second one get an os error

¿Perhaps it's this? ¿or the setence (for iterator in ..) it's trying to paralelize the work? How i can know how many threads are in a rust code

I was thinking that i was only doing async work not paralel work, and i don't have any join in the code because i preffer only one thread and in async way to another programs (and to learn async in rust)

If you .await, then it will wait for the thing and it will not continue until it is done. For loops will also not parallelize automatically. Maybe you should just use break instead of a variable to exit the loop?

Not, with a break sometimes the same happens

Sometimes i get the error of min_size_file, delete the directory and don't panic OK

but sometimes in the terminal:

[src/main.rs:242] imagen.attr("src").unwrap() = "//l.mangatown.com/store/manga/29123/010.0/compressed/p006_-005.jpg?token=073210eafa6b8f182ce51c84b1aebc0f9eb92a0e&ttl=1585141200"
[src/main.rs:302] &url_imagen = "//l.mangatown.com/store/manga/29123/010.0/compressed/p006
-005.jpg?token=073210eafa6b8f182ce51c84b1aebc0f9eb92a0e&ttl=1585141200"
Fichero muy pequeño menor, borrar directorio
Se ha borrado el directorio
[src/main.rs:242] imagen.attr("src").unwrap() = "//l.mangatown.com/store/manga/29123/010.0/compressed/p007
-006.jpg?token=9a2fcaefb96042f7b8a959c7771588b680a1acb4&ttl=1585141200"
[src/main.rs:302] &url_imagen = "//l.mangatown.com/store/manga/29123/010.0/compressed/p007
-_006.jpg?token=9a2fcaefb96042f7b8a959c7771588b680a1acb4&ttl=1585141200"
Error al crear fichero, borrar directorio
Error: No such file or directory (os error 2)
No he podido borrar directorio
Error: No such file or directory (os error 2)
thread 'main' panicked at 'explicit panic', src/main.rs:41:5
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

the code with the break:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2580b3c4293bb43d3dea5035094743c8

i know that i can do the code in more rustician way but i want to know the origin of the problem..

OK, It was my mistake,l because now i'm learning about the error how and WERE to catch in an async world

the problem were that i have capturred the error very inside of the functions, the superior function thinks that all is allright and tries to get the next page, and calls to get the new image, but the directory is missing, and get and os error (only 1 thread a time).

with this new knowledge i will try to clean the code in more rust way

i change the topic to solved