Update Examples

Glen Whitney 2024-08-25 03:25:53 +00:00
parent 9f8827d20f
commit 5a1b018006

@ -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
<table>
<tr>
@ -649,3 +648,74 @@ fn main
```
</td></tr></table>
### Flow of Control
#### if/else
<table>
<tr>
<th>Rust</th>
<th>Husht</th>
</tr>
<tr>
<td>
```
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);
}
```
</td><td>
```
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
```
</td></tr></table>