Urgent.News

What's breaking now, across thousands of outlets.

Tech

Blog Series: From TypeScript to Rust & Go — Day 3: Mastering Functions and Error Handling

Blog Series: From TypeScript to Rust & Go — Day 3: Mastering Functions and Error Handling Day 3: Writing Reusable Functions and Handling Errors in Rust and Go Introduction Welcome to Day 3 of our twice-weekly journey into Rust and Go! Today, we’ll explore functions and error handling , which are essential tools for writing clean, maintainable, and robust backend code. By the end of this post,…

Day 3 of our Rust and Go exploration focuses on functions and error handling, crucial skills for crafting reliable backend code. By mastering these concepts, developers can write maintainable and robust applications. Let's examine how these programming languages approach functions and error handling.

In both Rust and Go, functions serve as reusable blocks of code that encapsulate logic, preventing duplication. Function syntax varies between the languages: Rust uses `fn` followed by the function name, while Go uses `func` with the type specified after the parameter name. Both languages require explicit type annotations for parameters and return values.

Error handling is equally important. Rust employs the `Result` enum, which can be either `Ok` (representing success) or `Err` (indicating an error). Developers must handle errors explicitly using patterns like `match` or the `?` operator. Go, on the other hand, returns two values: a result and an error. Errors are typically handled immediately following a function call, enhancing code readability.

To reinforce these concepts, we built a simple calculator program in both Rust and Go. The calculator supports basic arithmetic operations like addition, subtraction, multiplication, and division, with error handling for invalid inputs. In Rust, the `Result` type is used to manage potential errors, while Go returns error values directly from functions.

By understanding these patterns in Rust and Go, developers can write cleaner, more maintainable code, bridging the gap from TypeScript to these powerful systems programming languages.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

How Cadastral Maps Work: Understanding GIS, GeoJSON, and Digital Land Mapping

Have you ever wondered how digital maps represent individual plots of land, their boundaries, neighboring properties, and geographical locations?

  • Cadastral maps show individual land parcels with boundaries and ownership records
  • Geographic Information Systems (GIS) enable digital land mapping and analysis
  • GeoJSON format represents geographical objects like land parcels as polygons

Docker Hub pull rate limit in GitHub Actions

A Docker pull rate limit in GitHub Actions is an HTTP 429 on an image your job is pulling, and for anonymous pulls it is counted per address, which on hosted runners is an address you share with…

Take a trip

Breathe Together: a camera-driven breathing coach where two people share one pacer Presage SmartSpectra SDK Challenge Submission Usman Adamu Usman Adamu Usman Adamu Follow Nov 9 Breathe Together: a…

More from Sunday 15 November →