# if expression

A Rust conditional expression that evaluates condition operands and executes a consequent or appropriate else block, producing the executed block's value or ().

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

## 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>), [if expression](<https://devfeed.tech/topics/if-expression.md>), [if statement](<https://devfeed.tech/topics/if-statement.md>), [typesafety](<https://devfeed.tech/topics/typesafety.md>), [Learning](<https://devfeed.tech/topics/learning.md>)

Tags: [directory](<https://devfeed.tech/tags/directory.md>), [rust](<https://devfeed.tech/tags/rust.md>), [rust-programming](<https://devfeed.tech/tags/rust-programming.md>)

### AI overview

The excerpt shows a beginner Rust program that checks whether `/home` is a directory using `Path::new(path).is_dir()` to distinguish a presumed Linux environment from other systems. Its first attempt fails because variables declared inside the `if` and `else` blocks are out of scope after those blocks. Moving the choice into `get_env()` fixes the scope issue, but the function initially lacks a return type; declaring `-> &'static str` lets it return the string literals `"unix"` or `"other"` and lets `main` print the result. The author presents this as a learning example based on the assumption that Linux home directories are under `/home` and Mac home directories under `/Users`; the excerpt does not show a completed Mac-specific check.

### 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