Week 01 Revision Questions

  1. Do I need to set up Rust on my own computer, or can I just use VLAB / CSE?

    How could I go about setting up Rust on my own computer?

  2. Let's write our first Rust program, hello world!

    First we'll write it as a main.rs file, and use the rustc command to compile it.

    Then, we will modify our program to be a cargo project instead.

    What are the differences in the two approaches? When should we use each approach in COMP6991?

  3. Translate the following C program into Rust:

    #include <stdio.h>
    
    int main(void) {
        int my_integer = 42;
    
        if (my_integer > 0) {
            printf("%d is positive\n", my_integer);
        } else if (my_integer < 0) {
            printf("%d is negative\n", my_integer);
        } else {
            printf("%d is zero\n", my_integer);
        }
    
        return 0;
    }
    
  4. Translate the following C program into Rust:

    #include <stdio.h>
    
    int main(void) {
        int count = 0;
    
        while (count < 10) {
            printf("%d\n", count);
            count++;
        }
    
        return 0;
    }
    
  5. Translate the following C program into Rust:

    #include <stdio.h>
    
    int main(void) {
        for (int count = 0; count < 10; count++) {
            printf("%d\n", count);
        }
    }
    

    Is it possible to translate this approach directly to Rust? Why / why not?

  6. Translate the following C program into Rust:

    #include <stdio.h>
    
    void my_function(int *x);
    
    int main(void) {
        int x = 42;
    
        my_function(NULL);
        my_function(&x);
    
        return 0;
    }
    
    void my_function(int *x) {
        if (x == NULL) {
            printf("no value for x\n");
        } else {
            printf("x has value %d\n", *x);
        }
    }
    

    Is this program possible to write in Rust? Discuss whether this is an advantage or disadvantage of Rust.