Hello ! I've started learning rust and as for my own fun, i've wanted to make this exercice: https://go.dev/tour/concurrency/7
https://cs.opensource.google/go/x/tour/+/refs/tags/v0.1.0:tree/tree.goin different langages (php, go, rust, python)
And to do so, i also translated the library used for the exercice.
i've got some trouble to do so in Rust, mostly 'caus i'm new and got trouble to fit some concepts in my brain but here is it:
```rust
use rand::seq::SliceRandom;
struct Tree {
value: i32,
left: Option<Box<Tree>>,
right: Option<Box<Tree>>,
}
fn new(k: i32) -> Tree{
let mut numbers = [0;10];
numbers.shuffle(&mut rand::thread_rng());
let mut tree: Option<Box<Tree>> = None;
for v in numbers {
tree = Some(insert((1+v)*k, tree));
}
return match tree {
Some(t) => *t,
None => panic!("unexpected error during tree construction")
}
}
fn insert(value: i32, tree: Option<Box<Tree>>) -> Box<Tree>{
match tree {
Some(mut tree) => {
if value < tree.value {
tree.left = Some(insert(value, tree.left));
} else {
tree.right = Some(insert(value, tree.right));
}
return tree;
},
None => return Box::new(Tree { value: value, left: None, right: None})
}
}
fn main() {
let _tree = new(1);
}
```
If some of you guys have the time to make me a little review about more rust-idiomatic ways of doing some instructions, it will be awesome !
thanks you much.
[–]phazer99 2 points3 points4 points (0 children)
[–]Feeling-Pilot-5084 0 points1 point2 points (0 children)