Learn Rust Flashcards

1
Q

Garbage Collection

A

None - frees up memory automatically

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Cargo

A
cargo new demoName
cargo build
cargo build --release
cargo run --release
cargo watch -x run
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Scalar Types: integer, floating point numbers, boolean, , characters

A

large whole number, floating point = decimals, boolean: true/false; characters:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

tuple type

A

(5, 5.0, “chris”). let (x, y, z) = tuple | x = 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
fn hello_world(name: &str) {
    println!("hello {}", name);
}
A

need & to know length of string else it wont compile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

i8

A

8 bit integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

::

A

bind method to call that library

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

u64

A

unsigned integer, number cant be negative

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

float

A

f32 floating number 6.7 with decimal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

if else statement

A

if n < 30 {} else {} | ==; !=; ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

enum

A

enum Name {variance} eg {Up, Down}. let test:Name = Name::Up

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

constants

A

const Name: u8 = 20; name and type. for n in 1..Name {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

tuples

A

bunch of variables in 1 place. let tup1 = (20, “test”, 21.1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

code block

A
fn main(){
   let x = 10; 
   {inside here can access outside x
   }
   outside here cant access inside {}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

shadowing

A
let mut x = 10;
{ let x = 15; }
x is 10 here. Inside code block is isolated.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

references

A
another way to refer to a variable. eg domenic is also dom. 
let mut x = 10; let xr = &x; cant change xr though.
eg let dom = &mut x; 
*dom += 1; can now change dom.
{eg let dom = &mut x; 
*dom += 1; can now change dom.}
17
Q

structs

A
group similar info in same place
struct Color {
  red: u8, 
  green: u8,
  blue: u8
}
fn main(){
let bg = Color {red: 255, green: 70, blue: 15};
18
Q

tuple struct

A
struct Color(u8, u8, u8);
fn main(){
let red = Color(255, 0, 0);
println!("{}", red.0, red.1, red.2);
19
Q

Pass by reference

A
struct Color {red: u8, green: u8, blue: u8}
fn main(){let blue = Color{red: 0, green: 0, blue: 255};
  print_color(&blue);
}
fn print_color(c: &Color){
println!("Color - R:{} G:{}, B{}", c.red, c.green, c.blue);}
& means reference to it
20
Q

Impl Keyword

A
struct Rectangle {width: u32, height: u32}
impl Rectangle {
  fn print_description(&self) // &self gives access to itself
  println!("Rectangle: {} x {}}, self.width, self.height); }
  fn is_square(&self) -> bool {self.width == self.height }
}
fn main(){ let my_rect = Rectangle {width: 10, height: 5};
  my_rect.print_description(); // no need to pass in anything as its done automatically in &self
  println!("Retangle is a squre: {}", my_rect.is_square());
}
21
Q

String

A
fn main(){
  let my_string = String::from("This is a string"); // variable my_string has data type String
}
22
Q

Traits

A
Something like an object, struct etc can do
struct Person {name: String, age: u8}
impl ToString for Person {
  fn to_string(&self) -> String {
    return format!("My name is {} and I am {}.}, self.name, self.age);
fn main(){
  let dom = Person {name: String::from("Domenic"), age: 21};
  println!("{}", dom.to_string()); }
23
Q

Vector

A

let my_vector = vec![1, 2, 3, 4];

24
Q

Defining Traits

A
trail certain set of rules/requirements, for that object/struct must have in order to have that name of the trait
struct Person {name: String, age: u8}
trait HasVoiceBox {
  // speak
  fn speak(&self); // if you have a voice box, you can speak
  // Check if can speak
  fn can_speak(&self) -> bool; }
// implement this trait on Person struct
impl HasVoiceBox for Person {
  fn speak(&self) {
    println!("Hello my name is {}", self.name);
  }
  fn can_speak(&self) -> bool{ if self.age > 0 {return true;} retnr false; }
  }
}
fn main(){
  let person = Person{name: String::from("Bob"), age: 41 }
  println!("Can {} speak? {}", person.name, person.can_speak()); 
  person.speak();
}
25
Q

Switch Statement - Pattern Matching

A
fn main(){
  let number = 2;
  match number {
    1 => println!("It is one"),
    2 => println!("There is 2"),
    10 || 11 => println!("or function"),
    2...20 => println!("2 to 20"),
    "Chris" => println!("Can match strings too"),
    _ => println!("It doesnt match") // last case _
}
26
Q

Modules (mod keyword)

A
mod decode {
  fn chicken(){println!("Chicken");}
  pub fn print_message(){
    chicken();    
    println!("Hows it going");}
  pub mod water {
    pub fn print_message(){println!("Im water");}
}
fn main(){
  dcode::print_message(); 
  dcode::water::print_message(); 
}
27
Q

Option (Enum)

A

fn main(){
let name = String::from(“Domenic”);
println!(“Character at index 8: {}”, match name.chars().nth(8) {
Some(c) => c.to_String(),
None => “No character at index 8!”.to_string()
}); }
OR
fn main() {
println!(“Occupation is {}”, match get_occupation(“Domenic”){
Some(o) => o,
None => “No occupation found” }); }
fn get_occupation(name: &str) -> Option { // name is type string, returns an option Enum of type string
match name {
“Domenic” => Some(“Software Developer”),
“Michael” => Some(“Dentist”),
_ => None } }

28
Q

Enum Methods

A
enum Day {Monday, Tues}
impl Day {
  fn is_weekday(&self) -> bool {
    match self { &Day::Saturday | &Day::Sunday => return false // 1 pipe |
                         _ => return true }}}
fn main(){ let d = Day::Tuesday;
  println!("Is d a weeday? {}", d.is_weekday()); }