An error occurred while trying to read the file

I am writing a web application, but there is an error when reading the file, saying that I can't find the file and directory, but I have this file

use std::net::TcpListener;
use std::net::TcpStream;
use std::io::prelude::*;
use std::fs;

fn main() {
    let listener = TcpListener::bind("").unwrap();

    for stream in listener.incoming() {
        let stream = stream.unwrap();


fn handle_connection(mut stream: TcpStream) {
    let mut buffer = [0; 512]; buffer).unwrap();

    // println!("请求:{}", String::from_utf8_lossy(&buffer[..]));
    let contents = fs::read_to_string("hello.html").unwrap();

    let response = format!("HTT`预先格式化的文本`P/1.1 200 OK\r\n\r\n{}", contents);


When I run, the error log is as follows:

   Compiling webapp v0.1.0 (/Users/jameszow/Documents/RustWorkSpace/rustlibrary/webapp)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s
     Running `target/debug/webapp`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', webapp/src/
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/
   1: core::panicking::panic_fmt
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/
   2: core::result::unwrap_failed
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/
   3: core::result::Result<T,E>::unwrap
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/
   4: webapp::handle_connection
             at ./webapp/src/
   5: webapp::main
             at ./webapp/src/
   6: core::ops::function::FnOnce::call_once
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/ops/
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
The terminal process "cargo 'run', '--package', 'webapp', '--bin', 'webapp'" terminated with exit code: 101.

Terminal will be reused by tasks, press any key to close it.

This is my directory structure:

| ___|

hello.html file in the root directory

It looks like you ran cargo run --package webapp --bin webapp (either directly or through an IDE) from the rustlibrary directory. Have you tried running cargo run from the package root directory?

I have adjusted the directory and run it. I found that the request can be made, but the response has not been responded, and the page has not been rendered

How are you making the request? On my own system, telnet receives a response with no issue.

The effect I want is to open the browser and visit to see hello HTML page

Have you tried removing the `预先格式化的文本` in the middle of the response header? I can view the hello.html response on Firefox if response starts with "HTTP/1.1 200 OK\r\n\r\n". You haven't said which browser you're using, so I'm not sure how it would handle the malformed response string. Also, if it sends a request longer than 512 bytes, it could be getting confused.

I think I probably understand what you mean. I use Chrome browser. I'm considering whether this has anything to do with my HTTP/1.1 200 OK\r\n\r\n

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.