Generics Types, Traits, and Lifetimes Flashcards

1
Q

What are generics?

A

Abstract stand-ins for concrete types or other properties.

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

When defining a function that uses generics, where do we place the generic in the signature?

A

Where the parameter data types and return values are,

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

What are placed between <> in function signatures between the function name and the parameter list when using generics in a function?

A

Type name declarations.

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

What is an example of a function signature with generics?

A

fn largest(list: &[T]) -> T {}

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

What is an example of using generics in a struct definition?

A

struct Point {
x: T,
y: U,
}

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

What is an example of using generics with an enum definition?

A

enum Result {
Ok(T),
Err(E),
}

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

How does Rust avoid performance cost when using generics?

A

By using monomorphization.

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

What is monomorphization?

A

The process of turning generic code into specific code by filling in the concrete types that are used when compiled.

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

What do traits do?

A

Tell the Rust compiler about functionality a particular type has and can share with other types.

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

What are trait definitions?

A

A way to group method signatures together to define a set of behaviors necessary to accomplish some purpose.

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

What is the syntax for implementing a trait on a type?

A
impl  for  {
    fn foo(&amp;self) ->  {
       ...
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you use a default trait function on a type?

A

Specify an empty impl block . impl for {}

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

Provide an example of how to define a function that accepts many different types.

A

pub fn notify(item: impl Summary) {
println!(“Breaking news! {}”, item.summarize());
}

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

What is the impl trait syntactic sugar for?

A

trait bounds.

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

Where are trait bounds placed in a function signature?

A

With the declaration of the generic type parameter after a colon and inside angle brackets.

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

How can you force multiple generic parameters to have the same type?

A

Using trait bounds.

17
Q

How can you specify more than one trait bound?

A

Using the + syntax. e.g.
pub fn notify(item: impl Summary + Display) {
or
pub fn notify(item: T) {

18
Q

Whats an example of using the where clause with trait bounds?

A

fn some_function(t: T, u: U) -> i32
where T: Display + Clone,
U: Clone + Debug
{

19
Q

Whats an example of a return type that implements a trait in a function?

A

fn returns_summarizable() -> impl Summary {
Tweet {
username: String::from(“horse_ebooks”),
content: String::from(“hello world”)
}
}

20
Q

When a function implements a trait on a return type, how many types can you return?

A

exactly one.