Skip to content

duyoji/cc2-rust-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Setup environment

Install cargo

cargo is package manager of Rust. It is same as npm or yarn of JavaScript.

Open terminal, put following command and follow the process.

$ curl https://sh.rustup.rs -sSf | sh

Select "1" When Showing up following.

Current installation options:

   default host triple: x86_64-apple-darwin
     default toolchain: stable
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
  • Don't forget adding path of cargo showing up on terminal.
  • After finishing this process, you can use cargo command.

Run main program

$ cargo run

To run programs.

  • Add main.rs in src directory.
  • Create function named main in main.rs.
    • "fn main {}" is a entry point.

Run test

$ cargo test
  • A function that is annotated with #[test] is executed as a test code when running $ cargo test.
  • Put the annotation before fn keyword.

Basic Requirements

  • Translate below JavaScript code into Rust.
    • In order to run your program with cargo run, you need to create main.rs and a function named main in the file.
    • Hint: If you stuck with something from above code, you can refer slides of this lecture.
  • Difine type of return value and arugments on each function/method.
  • Write tests for each function/method.
function main() {
  const number_1 = 1;
  console.log(number_1);

  let number_2 = 1;
  number_2 += 1;
  console.log(number_2);

  const number_3 = add(1, 2);
  console.log(number_3);

  output('this is input value');

  const person = new Person(
    'Code',
    'Chrysalis',
    2
  );
  console.log(person);
  person.greet();
}

function add(a, b) {
  return a + b;
}

function output_string(input) {
  console.log(input);
}

class Person {
  constructor(firstName, lastName, age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
  }

  greet() {
    console.log(
      "My name is %s %s. I'm %d years old.",
      this.firstName,
      this.lastName,
      this.age
    );
  }
}

Advanced Requirements.

  • Separate functions and a struct into other files.
  • Serialize and deserialize your person instance with Serde.
    • add dependencies in Cargo.toml.
    • Output person information on your console with println!.
  • Create an array.(vector) of person instances and print each information with for in loop.
    • To output a person information, use Serde and derive what you need from Serde.

Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages