Section 1

Preview this deck

Create a simple hash map of two team scores (Blue:10 and Yellow:50).

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

3

All-time users

3

Favorites

0

Last updated

1 year ago

Date created

Mar 1, 2020

Cards (57)

Section 1

(50 cards)

Create a simple hash map of two team scores (Blue:10 and Yellow:50).

Front

use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50);

Back

Increment vector elems with for.

Front

let mut v = vec![1, 2, 3]; for i in &mut v { *i += 1; }

Back

How Do I Cross-compile In Rust?

Front

It is possible but need a bit of work to set up.

Back

How to define array in Rust?

Front

let x = [1, 2, 3];

Back

What is crate?

Front

Compilation unit.

Back

Simple way to grow a String!

Front

let mut s = String::from("Hello"); s.push_str(", world!");

Back

How to get() element of a vector?

Front

let v = vec![1, 2, 3]; match v.get(2) { Some(e) => println!("e: {}", e), None => println!("none"), }

Back

What happens if we use slice of string (String or string literal) in a wrong way?

Front

Panic in run time!

Back

Write declaration of "ToString" train ("std::string::ToString").

Front

pub trait ToString { fn to_string(&self) -> String; }

Back

What part of Rust implements &str type?

Front

Built-in type.

Back

How to borrow the whole array as a slice?

Front

let x = [1, 2, 3]; analyze_slice(&x);

Back

Print command line arguments.

Front

use std::env; for arg in env::args() { p!("{}", arg); }

Back

How to borrow the third element of a vector?

Front

let v = vec![1, 2, 3]; let third = &v[2]; print_third(third);

Back

What part of Rust implements String type?

Front

Standard library.

Back

How to get grapheme clusters using Rust standard library?

Front

No way. Use external crates.

Back

Does Rust Have Move Constructors?

Front

No, The values of all types are moved via memcpy.

Back

How to print a number in binary format?

Front

println!("{} of {:b} people know binary, the other half doesn't", 1, 2);

Back

What is "{:?}" in println macro?

Front

"fmt::Debug" uses the {:?} marker. Format text for debugging purposes.

Back

Crate containts an implicit and un-named top-level [xxx].

Front

module

Back

Print UTF-8 string byte-by-byte.

Front

for b in "abcБОРЩЬ".bytes() { println!("{}", b); }

Back

Does Rust Guarantee A Specific Data Layout?

Front

Not by default, Most of the general case, the enum and struct layouts are undefined.

Back

How to store elements of different type in vector?

Front

Vector of enums. enum SpreadCell { Int(i32), Text(String), } let v = vec![ SpreadCell::Int(3), SpreadCell::Text(String::from("blue")), };

Back

What is the array signature?

Front

[T; size]

Back

How to pad right-aligned number with extra zeros?

Front

println!("{num:>0width$}", num=1, width=6);

Back

For what kind of types the values are COPIED to hash map?

Front

For types that implement Copy trait (e.g., i32).

Back

What Are The Disadvantages?

Front

1) Rust compilation seems slow 2) Rust has a moderately-complex type system 3) The Rust compiler does not compile with optimizations unless asked to, as optimizations slow down compilation and are usually undesirable during development.

Back

Print vector elems with for.

Front

let v = vec![1, 2, 3]; for i in &v { println!("{}", i); }

Back

How to print simple array in Rust?

Front

let x = [1, 2, 3]; println!("Array is {:?}", x);

Back

What data types are stored on stack?

Front

Built-in array and tuples.

Back

Simple way to create an empty vector of i32?

Front

let v: Vec<i32> = Vec::new();

Back

When vector is freed?

Front

Like any other struct, a vector is freed when it goes out of scope.

Back

Signature of std::string::String::push_str(...)?

Front

pub fn push_str(&mut self, string: &str)

Back

How to init all array elements to the same value?

Front

let x = [0; 256];

Back

Is Rust Object Oriented?

Front

It is multi paradigm and most of the things can do in Rust but not everything. So, Rust is not object-oriented.

Back

Create simple vector with 1, 2 and 3 using vector macro!

Front

let v = vec![1, 2, 3];

Back

What Is The Relationship Between A Module And A Crate?

Front

Module - UNIT of code organization inside a crate. Crate - compilation UNIT.

Back

For what kind of types the values are MOVED to hash map (and hash map becomes a new owner of those values)?

Front

owned types that do NOT implement Copy trait (like std::String)

Back

What's the difference between string.chars() and string.bytes()?

Front

chars() - individual Unicode scalar values bytes() - raw bytes

Back

Name simple methods to iterate over strings.

Front

for c in "xxx".chars() { ... } for b in "yyy".bytes() { ... }

Back

What is `std::string::ToString`?

Front

Trait for converting a value to a `String`.

Back

Create empty vector and add one i32 number.

Front

let mut v = Vec::new(); v.push(5);

Back

The idiomatic way of printing slice/array in hexadecimal?

Front

let ciphertext: [u8; 256] = [0; 256]; println!("ciphertext is: {:?}", ciphertext);

Back

How to print array size in bytes?

Front

use std::mem; let x = [1, 2, 3]; println!("{} bytes", mem::size_of_val(&xs));

Back

How to print with named arguments?

Front

println!("{first} {sec}", first="xxx", sec="yyy");

Back

How to print array size?

Front

let x = [1, 2, 3]; println!("len: {}", x.len());

Back

How to convert any type (that implements Display trait) to string?

Front

let data = "initial contents"; let s = data.to_string();

Back

What data types are stored on heap?

Front

std::collections (vector, string, hash map and other).

Back

How to print right-aligned text?

Front

println!("{num:>width$}", num=1, width=6);

Back

What Are The Differences Between The Two Different String Types?

Front

1) The "String" is an owned buffer of UTF-8 bytes allocated on the heap. 2) The "Strings" are Mutable and it can be modified. 3) The "&str" is a primitive type and it is implemented by the Rust language while String is implemented in the standard library.

Back

How to print with positional arguments?

Front

println!("B:{1}, A:{0}", "Alice", "Bob");

Back

Section 2

(7 cards)

Give a very simple example with "ToString" trait for "String" and "i32" using "assert_eq!(...)".

Front

let n = 5; let five = String::from("5"); assert_eq!(five, n.to_string());

Back

What is the limitation for the below code? for x in &array { }

Front

It works __only__ if the array has 32 or fewer elements.

Back

What is the relation between "Display" trait and "ToString" trait?

Front

This trait is *automatically* implemented for any type which implements the Display trait.

Back

In what way "ToString" should (or should NOT) be implemented?

Front

ToString shouldn't be implemented directly: Display should be implemented instead, and you get the ToString implementation for free.

Back

What will happen and why? let array: [i32; 3] = [0; 3]; for x in array { ... }

Front

An array itself is not iterable.

Back

What kind of citizens are arrays in Rust (and WHY)?

Front

Second-class citizens as it is not possible to form array generics.

Back

What "unusual" module is always contained within __any__ crate?

Front

and it contains an implicit and un-named top-level module.

Back