Removed C style flow control

This commit is contained in:
2026-06-10 04:35:11 +02:00
parent 286894c5db
commit afd3d05e2f
10 changed files with 42 additions and 111 deletions
+3 -3
View File
@@ -99,17 +99,17 @@ pub fn test_block_expr() {
}
pub fn test_if_expr() {
let x = if (true) { 1 } else { 2 };
let x = if true { 1 } else { 2 };
assert_eq!(x, 1);
let y = if (false) { 1 } else { 2 };
let y = if false { 1 } else { 2 };
assert_eq!(y, 2);
}
pub fn test_range_expr() {
let r = 0..5;
let mut count = 0;
for (i : r) {
for i in r {
count++;
}
assert_eq!(count, 5);
+14 -14
View File
@@ -18,12 +18,12 @@ pub fn test_var_decl_with_init() {
pub fn test_if_statement() {
let mut x = 0;
if (true) {
if true {
x = 1;
}
assert_eq!(x, 1);
if (false) {
if false {
x = 2;
} else {
x = 3;
@@ -34,9 +34,9 @@ pub fn test_if_statement() {
pub fn test_if_else_if() {
let mut x = 0;
if (x == 1) {
if x == 1 {
x = 10;
} else if (x == 0) {
} else if x == 0 {
x = 20;
} else {
x = 30;
@@ -47,7 +47,7 @@ pub fn test_if_else_if() {
pub fn test_while_loop() {
let mut count = 0;
while (count < 5) {
while count < 5 {
count++;
}
assert_eq!(count, 5);
@@ -56,7 +56,7 @@ pub fn test_while_loop() {
pub fn test_for_loop() {
let mut sum = 0;
for (i : 0..4) {
for i in 0..4 {
sum += i;
}
assert_eq!(sum, 6);
@@ -67,7 +67,7 @@ pub fn test_loop_stmt() {
loop {
count++;
if (count >= 3) {
if count >= 3 {
break;
}
}
@@ -77,11 +77,11 @@ pub fn test_loop_stmt() {
pub fn test_break_continue() {
let mut sum = 0;
for (i : 0..10) {
if (i % 2 == 0) {
for i in 0..10 {
if i % 2 == 0 {
continue;
}
if (i >= 7) {
if i >= 7 {
break;
}
sum += i;
@@ -105,7 +105,7 @@ pub fn test_match_literal() {
let x = 2;
let result;
match (x) {
match x {
1 => { result = 10; }
2 => { result = 20; }
3 => { result = 30; }
@@ -118,7 +118,7 @@ pub fn test_match_or_pattern() {
let x = 1;
let result i32;
match (x) {
match x {
1 | 2 => { result = 100; }
3 => { result = 200; }
_ => panic!();
@@ -130,7 +130,7 @@ pub fn test_match_default() {
let x = 99;
let result;
match (x) {
match x {
1 => { result = 10; }
y => { result = y; }
}
@@ -141,7 +141,7 @@ pub fn test_for_with_pattern_destructure() {
let pairs = [(1, "a"), (2, "b")];
let mut keys = 0;
for ([k, _] : pairs) {
for [k, _] in pairs {
keys += k;
}
assert_eq!(keys, 3);
+5 -5
View File
@@ -5,7 +5,7 @@ enum OptionInt {
pub fn test_enum_named() {
let x = OptionInt::None;
match (x) {
match x {
OptionInt::None => { assert_eq!(1, 1); }
OptionInt::Some[_] => { assert_eq!(0, 1); }
}
@@ -13,7 +13,7 @@ pub fn test_enum_named() {
pub fn test_enum_tuple() {
let x = OptionInt::Some(42);
match (x) {
match x {
OptionInt::None => { assert_eq!(0, 1); }
OptionInt::Some[v] => { assert_eq!(v, 42); }
}
@@ -26,13 +26,13 @@ enum Shape {
pub fn test_enum_struct() {
let c = Shape::Circle { radius: 5 };
match (c) {
match c {
Shape::Circle { radius } => { assert_eq!(radius, 5); }
Shape::Rect { .. } => { assert_eq!(0, 1); }
}
let r = Shape::Rect { w: 3, h: 4 };
match (r) {
match r {
Shape::Circle { .. } => { assert_eq!(0, 1); }
Shape::Rect { w, h } => { assert_eq!(w * h, 12); }
}
@@ -46,7 +46,7 @@ enum Color {
pub fn test_enum_eq() {
let c = Color::Red;
match (c) {
match c {
Color::Red => { assert_eq!(1, 1); }
_ => { assert_eq!(0, 1); }
}
+2 -2
View File
@@ -44,8 +44,8 @@ pub fn test_nested_fn_calls() {
}
fn factorial(n i32) i32 {
if (n <= 1) 1
else n * factorial(n - 1)
if n <= 1 { 1 }
else { n * factorial(n - 1) }
}
pub fn test_recursive_fn() {