From 5a1b018006dde90a5a7b355d6f8cadb6c08faf59 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sun, 25 Aug 2024 03:25:53 +0000 Subject: [PATCH] Update Examples --- Examples.md | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/Examples.md b/Examples.md index d812200..f564a63 100644 --- a/Examples.md +++ b/Examples.md @@ -1,5 +1,4 @@ -This page selects some example programs from "Rust by Example" and shows their intended Husht equivalents. (Of course, the details of the syntax on the Husht side may end up varying in some cases for practical reasons from these initial proposals. The examples will be updated when and if they change, and will be used as cases in the eventual Husht test suite.) - +This page selects some example programs from "Rust by Example" and shows their intended Husht equivalents. (Of course, the details of the syntax on the Husht side may end up varying in some cases for practical reasons from these initial proposals. The examples will be updated when and if they change, and will be used as cases in the eventual Husht test suite.) Comments included in the Husht versions to add detail/explanation are marked with `//!`. ### Hello World @@ -649,3 +648,74 @@ fn main ```
+### Flow of Control + +#### if/else + + + + + + +
RustHusht
+ +``` +fn main() { + let n = 5; + + if n < 0 { + print!("{} is negative", n); + } else if n > 0 { + print!("{} is positive", n); + } else { + print!("{} is zero", n); + } + + let big_n = + if n < 10 && n > -10 { + println!(", and is a small number, increase ten-fold"); + // This expression returns an `i32`. + 10 * n + } else { + println!(", and is a big number, halve the number"); + // So here must return `i32` as well. + n / 2 + // TODO ^ Try inserting a semicolon. + }; + // ^ Don't forget to put a semicolon here! All `let` bindings need it. + + println!("{} -> {}", n, big_n); +} +``` + + +``` +fn main + let n = 5 + + //! Note for the "then"-clause to be on the same line + //! as the condition requires an explicit `then` + if n < 0 + print! "{} is negative", n + else if n > 0 then print! "{} is positive", n + else print! "{} is zero", n + + //! Note more literate logical operators + let big_n = + if n < 10 and n > -10 + println! ", and is a small number, increase ten-fold" + // This expression returns an `i32`. + 10 * n + else + println! ", and is a big number, halve the number" + // So here must return `i32` as well. + n / 2 + // TODO ^ Try inserting a semicolon. + + // ^ Don't forget to put a semicolon here! All `let` bindings need it. + //! ^ Husht FTW! + + println! "{} -> {}", n, big_n + +``` +
\ No newline at end of file