Hello Guys! In this code I have to create an algorithm : if v_speed <= -41 than do this, if v_speed >= -41 than do this. Someone can help me?

use std::io;

macro_rules! parse_input {
($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())

fn main() {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let surface_n = parse_input!(input_line, i32); // the number of points used to draw the surface of Mars.
for i in 0..surface_n as usize {
    let mut input_line = String::new();
    io::stdin().read_line(&mut input_line).unwrap();
    let inputs = input_line.split(" ").collect::<Vec<_>>();
    let land_x = parse_input!(inputs[0], i32); // X coordinate of a surface point. (0 to 6999)
    let land_y = parse_input!(inputs[1], i32); // Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars.

// game loop
  loop {

    let mut input_line = String::new();
    io::stdin().read_line(&mut input_line).unwrap();
    let inputs = input_line.split(" ").collect::<Vec<_>>();
    let x = parse_input!(inputs[0], i32);
    let y = parse_input!(inputs[1], i32);
    let h_speed = parse_input!(inputs[2], i32); // the horizontal speed (in m/s), can be negative.
    let v_speed = parse_input!(inputs[3], i32); // the vertical speed (in m/s), can be negative.
    let fuel = parse_input!(inputs[4], i32); // the quantity of remaining fuel in liters.
    let rotate = parse_input!(inputs[5], i32); // the rotation angle in degrees (-90 to 90).
    let power = parse_input!(inputs[6], i32); // the thrust power (0 to 4).


hmm, honestly, this looks more like a math or physics than a programming exercise. If I understand it well, you could just have a single function taking location, speed, fuel and returning trust level and angle.

That doesn't use a whole lot of rust functionality. You might want to return a tuple, but that's about as far as I get for rust concepts...

1 Like

Thank you najamelan. I found the solution.
let p = power;
let p = 0;
if v_speed <= -40 {
println!("0 {}", p + 4)
}else if v_speed >= -40 {
println!("0 {}", p)
I wanted the power value to be 0 until v_speed reach -40 ,after v_speed has reaching -40,than change the power to 4.

1 Like

Nice you found it. Looked quite fun, with the image of the mars rover and everything.

If you want to hone your Rust skills, I can suggest exercism.io It will walk you through more rust specific topics, but it's not as fancy (no images).

I appreciate your suggestion :smiley:

In exercism.io website I cant configure the CLI exercism configure --token=xxx
The terminal error Could not find the database of available applications, run update-command-not-found as root to fix this
i have installed snap and exercism too. any fix for this ?

Just a note, you can use code fences to format your code

// your code here

In place of rust you can use most programming languages or other text formats. If you don't put anything, it assumes Rust by default

like diff

- hllo world
+ hello world

or cpp

class Foo {}

switch(x) { case 0: break; }

goto label;

and many more

Did you try which exercism? Verify it's in your path. And did you run the suggested update-command-not-found as root?

1 Like

Two questions about the code:

  1. Why do you assign power to p, and then instantly redefine it as 0?
  2. Why are the comparisons <= and >=? If v_speed is exactly -40 it will go into the first branch of the if, the condition on the else can be if v_speed > -40. This is a more correct specification of what the program is supposed to do.

yes i run it as root, nothing changed. i will fix it later :smiley:

I am just a beginner in programming(And I choose Rust,.for 2 months i have already learned much.)
Thank you Tnleeuw for your correction !

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.