# Rust Programming

Published articles for Rust Programming.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Transitioning from C and C++ to Rust: A Beginner's Guide

DevFeed: [Transitioning from C and C++ to Rust: A Beginner's Guide](<https://devfeed.tech/articles/transitioning-from-c-and-c-to-rust-a-beginner-s-guide-22268.md>)

Original publisher: [Read original article](<https://www.ardanlabs.com/blog/2024/10/transitioning-from-c-and-c++-to-rust.html>)

Published: 2024-10-29T00:00:00Z

Content type: tutorial

Language: en

Sources: [William Kennedy](<https://devfeed.tech/sources/william-kennedy.md>)

Topics: [Rust](<https://devfeed.tech/topics/rust.md>), [C](<https://devfeed.tech/topics/c.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [beginner](<https://devfeed.tech/tags/beginner.md>), [c](<https://devfeed.tech/tags/c.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [c-plus-plus-to-rust](<https://devfeed.tech/tags/c-plus-plus-to-rust.md>), [c-to-rust](<https://devfeed.tech/tags/c-to-rust.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [enums](<https://devfeed.tech/tags/enums.md>), [generics](<https://devfeed.tech/tags/generics.md>), [guide](<https://devfeed.tech/tags/guide.md>), [memory-management](<https://devfeed.tech/tags/memory-management.md>), [memory-safety](<https://devfeed.tech/tags/memory-safety.md>), [migrating-from-c-plus-plus-to-rust](<https://devfeed.tech/tags/migrating-from-c-plus-plus-to-rust.md>), [pattern-matching](<https://devfeed.tech/tags/pattern-matching.md>), [pointers](<https://devfeed.tech/tags/pointers.md>), [runtime-errors](<https://devfeed.tech/tags/runtime-errors.md>), [rust](<https://devfeed.tech/tags/rust.md>), [rust-for-c-developers](<https://devfeed.tech/tags/rust-for-c-developers.md>), [rust-for-c-plus-plus-programmers](<https://devfeed.tech/tags/rust-for-c-plus-plus-programmers.md>), [rust-programming](<https://devfeed.tech/tags/rust-programming.md>), [transitioning-from-c-and-c-plus-plus-to-rust-guide](<https://devfeed.tech/tags/transitioning-from-c-and-c-plus-plus-to-rust-guide.md>), [transitioning-from-c-and-c-plus-plus-to-rust-tutorial](<https://devfeed.tech/tags/transitioning-from-c-and-c-plus-plus-to-rust-tutorial.md>), [transitioning-to-rust](<https://devfeed.tech/tags/transitioning-to-rust.md>), [types](<https://devfeed.tech/tags/types.md>)

### AI overview

A beginner's guide to transitioning from C and C++ to Rust, focusing on Rust's ownership and borrowing model, compile-time memory-safety checks, and type-system features such as enums, pattern matching, and generics.

### Source excerpt

Introduction: As you embark on the journey from C or C++ to Rust, you'll discover a world of exciting possibilities. Rust's emphasis on safety, concurrency, and performance can significantly enhance your programming toolkit. This beginner's guide on transitioning from C and C++ to Rust will provide a structured approach to making that transition, addressing essential concepts and practical applications. Let's dive in! Step 1: Understanding Rust's Ownership Model Concept Overview: Rust's ownership model is its most distinctive feature. Unlike C and C++, where you have pointers and manual memory management, Rust uses a system of ownership with rules that the compiler checks at compile time.

## Simple Rust Programming 01 - Checking The Existence of a Directory

DevFeed: [Simple Rust Programming 01 - Checking The Existence of a Directory](<https://devfeed.tech/articles/simple-rust-programming-01-checking-the-existence-of-a-directory-28322.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rust/2020/06/03/simple-rust-programs-01-checking-the-existence-of-a-directory.html>)

Author: Fuzzygroup

Published: 2020-06-03T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Rust](<https://devfeed.tech/topics/rust.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Code](<https://devfeed.tech/topics/code.md>), [Linux](<https://devfeed.tech/topics/linux.md>), [Unix](<https://devfeed.tech/topics/unix.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [linux](<https://devfeed.tech/tags/linux.md>), [programming](<https://devfeed.tech/tags/programming.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [rust](<https://devfeed.tech/tags/rust.md>), [rust-programming](<https://devfeed.tech/tags/rust-programming.md>), [unix](<https://devfeed.tech/tags/unix.md>)

### AI overview

A beginner Rust tutorial develops a small program that checks whether a directory exists to distinguish Mac and Linux environments. It explains scope errors, refactoring environment lookup into a function, and specifying return types.

### Source excerpt

So I'm at the very early learning stage of Rust, that stage where almost nothing you type in works. And this, for me, usually means "make the learning stick by writing it down". So here goes ... Note: I wrote this code using the Rust playground, a web based repl, which was invaluable. A Simple Program for Testing the Existence of a Directory I'm going to start with an assumption that I need to get the location of my input based on the type of the machine: either a Mac or a Linux box. Given that we know that a user's home directory exists in /home on Linux and /Users on Mac, this gives us an easy way to differentiate the two. Pass 1: Problems with Scopes use std::path::Path; fn main() { if path_exists("/home") { let environment = "unix"; } else { let environment = "other"; } println!("{}", environment); } fn path_exists(path: &str) -> bool { if Path::new(path).is_dir() { return true; } else { return false } } This program has a core problem - the environment variable isn't in scope: Compiling playground v0.0.1 (/playground) error[E0425]: cannot find value `environment` in this scope --> src/main.rs:11:20 | 11 | println!("{}", environment); | ^^^^^^^^^^^ not found in this scope The implication of this is that code within { } braces is a true local block and thus out of scope with what came before. The Ruby work around for something like this is to have an outer variable that "shadows" the name of the variable within the block and thus allows its value to pass out. Unfortunately Rust identifies that technique as a bug since the outer variable is never read. My refactor on this is to write a function which returns this. Pass 2: Problems with Return Types Here was my first pass at moving environment fetching into a function of its own. I got tired of typing environment so I moved to calling it my_env. use std::path::Path; fn main() { let my_env = get_env(); println!("{}", my_env); } fn get_env() { if path_exists("/home") { return "unix"; } else { return "other"; } } fn pat