Giter Club home page Giter Club logo

rust-dev-guide's Introduction

logo

About alexzanderr

Hello im a systems programmer and I love the Rust programming language.

skills:

  • rust
  • C, C++
  • shell script (bash, zsh)
  • html, css

setup this cool readme on your profile

  • create a repo and name it to your <username>
  • place a README.md file in the repo
  • the markdown will be visible on your profile to anyone

rust-dev-guide's People

Contributors

dragoshel avatar

Watchers

 avatar

rust-dev-guide's Issues

How do I return a custom Result for error handling in Rust just like how in Python you can throw your own custom Exceptions?

Rust differs from other programming languages on the topic of error handling. Where other languages put exceptions in one big pile, Rust makes a distinction between recoverable and unrecoverable errors.

Recoverable errors are the kind of errors that we are familiar with:

  • Making an HTTP request when the URL is invalid or when there is no Internet.
  • Trying to read from a file that doesn't exist.

These kinds of errors can be recovered from because we can show the user a friendly error message and carry on with doing other stuff like rendering an interface.

Unrecoverable errors are a sign of bad programming like accessing a buffer beyond its capacity or they are the result of an action that was tried and cannot be repaired like accessing a protected file (there is nothing we can do if we don't have read and write access to file 'file.txt').

Yet again, Rust differs because it doesn't have an exception system like the one that we are familiar with: try-catch and throw Exception. Instead, we are given a Result enum that encapsulates the successful result of an operation in Ok, or the error upon a failure in Err.

try:
    file = open("file.txt", "r")
except FileNotFoundError:
    file = open("file.txt", "w")

The exception system in Python is simple and straightforward. In the above example, open in read mode throws a FileNotFoundError is the file doesn't exist.

let file = match fs::read_to_string("file.txt") {
    Ok(value) => value,
    Err(error) => match File::create("file.txt") {
        Ok(_) => {}
        Err(_) => panic!(),
    },
};

I like this approach better because a Result you can always return from a function, unlike traditional exceptions where you were usually returning the success but if you ever encountered an exception you had to think really hard what to do with it later down the line - propagate it, resolve it or wrap in your own custom exception. Something more, from analyzing the code, you can see exactly what part it's going to return a Result (and possibly an error) which makes reasoning about logic much easier and natural.

So far so good. Now what is the problem that I have with this?

In Python you can throw your own Exceptions, say if "file doesn't exist" throw MyOwnCustomException, but how would you do that in Rust?

So far, I've only been shown methods that return Result but never created my own logic that returns Result.

Can you give me an example of a Rust program that checks for some logic and returns result?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.