Greetings to all.

I have spent the last couple of evenings learning about Rust and trying it out. Wrote a simple cli calculator as a first thing and thought I would improve it by making it available over http.

I was actually a bit surprised to find that there was no http tooling in the standard library, and searching online gave me an overload of information on different libraries and frameworks.

I ended up implementing my own simple HTTP server, might as well as this is a learning project.

Now I have it working, and while it isn’t perfect or done, I thought that this would be a good time to check what things I am doing wrong/badly.

Which is why I am here, would love to get some pointers on it all to make sure I am going in the right direction in the future.

The project is hosted here: https://github.com/Tebro/rsimple_http

You are viewing a single thread.
View all comments
4 points

My quick notes which are tailored to beginners:

Use Option::ok_or_else() and Result::map_err() instead of let .. else.

  • let .. else didn’t always exist. And you might find that some old timers are slightly triggered by it.
  • Functional style is generally preferred, as long as it doesn’t effectively become a code obfuscater, like over-using Options as iterators (yes Options are iterators).
  • Familiarize yourself with the ? operator and the Try trait

Type inference and generic params

let headers: HashMap = header_pairs
        .iter()
        .map(|line| line.split_once(":").unwrap())
        .map(|(k, v)| (k.trim().to_string(), v.trim().to_string()))
        .collect();

(Borken sanitization will probably butcher this code, good thing the problem will be gone in Lemmy 0.19)

Three tips here:

  1. You don’t need to annotate the type here because it can be inferred since headers will be returned as a struct field, the type of which is already known.
  2. In this pattern, you should know that you can provide the collected type as a generic param to collect() itself. That may prove useful in other scenarios.
  3. You should know that you can collect to a Result/Option if the iterator items are Results/Options. So that .unwrap() is not an ergonomic necessity 😉

Minor point

  • Use .into() or .to_owned() for &str => String conversions.
    • Again, some pre-specialization old timers may get slightly triggered by it.

make good use of the crate echo system

  • It’s important to make good use of the crate echo system, and talking to people is important in doing that correctly and efficiently.
    • This post is a good start.
    • More specifically, the http crate is the compatibility layer used HTTP rust implementations. Check it out and maybe incorporate it into your experimental/educational code.

Alright, I will stop myself here.

permalink
report
reply
2 points

Thanks! Really good points here, will have to find some time to apply them.

permalink
report
parent
reply

Rust

!rust@programming.dev

Create post

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits
  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

Community stats

  • 607

    Monthly active users

  • 776

    Posts

  • 3.3K

    Comments