MCPcopy Create free account

hub / github.com/arendjr/rust-for-ts-devs / functions

Functions59 in github.com/arendjr/rust-for-ts-devs

↓ 2 callersFunctioncreate_struct_with_foo
# Example
src/structs.rs:12
↓ 2 callersFunctiondivide
`Result<T, E>` is mainly used to replace exceptions.
src/option_and_result.rs:14
↓ 2 callersFunctionmodify
In Rust terminology, `modify()` *borrows* a mutable reference.
src/borrowing.rs:18
↓ 2 callersFunctionpositive_n_result
(n: i32)
src/option_and_result_conversions.rs:5
↓ 2 callersFunctionprint
(b: &MyCopyableStruct)
src/borrowing.rs:22
↓ 2 callersFunctionprocess_and_print
Instead, we commonly use trait bounds:
src/function_traits.rs:2
↓ 2 callersFunctionprocess_and_print
(string: &str, processor: ProcessStr)
src/callbacks.rs:5
↓ 1 callersFunctionmodify
In Rust terminology, `modify()` *borrows* a mutable reference.
src/references.rs:13
↓ 1 callersFunctionmodify
(mut a: MyCopyableStruct)
src/mutability.rs:18
↓ 1 callersFunctionmodify_string
(string: Rc<Cell<String>>)
src/cells.rs:13
↓ 1 callersFunctionpositive_n
`Option<T>` is used in place of `T | null` or `T | undefined`.
src/option_and_result.rs:5
↓ 1 callersFunctionprint_string
(string: Rc<String>)
src/reference_counting.rs:18
↓ 1 callersFunctionquiz
Quiz time! (Use `cargo run` to see the answer)
src/mutability.rs:12
↓ 1 callersFunctionsecond_quiz
Let's do another quiz! Same as the first, but now we pass by reference:
src/references.rs:5
↓ 1 callersMethodset_foo
(&mut self, string: &str)
src/mutable_methods.rs:7
↓ 1 callersFunctionsplit
This one shows almost everything we learned today in one signature:
src/split_examples.rs:2
↓ 1 callersFunctionsplit_vec
But notice how we no longer need the lifetime of `delim` once we have collected the split strings into a `Vec`:
src/split_examples.rs:8
↓ 1 callersFunctionunit_function
# Example: Function without return value:
src/functions_and_tuples.rs:4
Functionadd
# Example
src/numbers.rs:16
Functionadd
(a: T, b: T)
src/where_clauses.rs:14
Methodadd
(self, rhs: Self)
src/traits.rs:18
Functioncalling_clone_solves_our_woes
()
src/clone_trait.rs:9
Functionclone
(item: &T)
src/generic_functions.rs:5
Functionclone_and_print
(item: &T)
src/where_clauses.rs:5
Functionclone_and_print
Little sidestep: `Clone` is part of Rust's "prelude" (the set of types that are in scope by default), but `Display` isn't. That's why we need to speci
src/generic_functions.rs:15
Functionconcatenate
# Example 1
src/strings.rs:10
Functiondivide_option
(x: i32, y: i32)
src/option_and_result_conversions.rs:9
Functiondivide_or_zero
(x: i32, y: i32)
src/option_and_result_conversions.rs:13
Methodfrom_points
(points: &'a [Point<f64>])
src/lifetimes.rs:17
Functionget_printable
`impl` can be used when you want to hide a concrete type. In this example, all we know is that this function returns "something" that implements the `
src/impl_and_dyn.rs:16
Functiongreeting
# Example 2
src/strings.rs:16
Functionhurray
()
src/copy_trait.rs:11
Functionhurray_for_closures
()
src/function_traits.rs:12
Functionhurray_for_maps
()
src/maps.rs:3
Functionhurray_for_rc
()
src/cells.rs:5
Functionhurray_for_rc
()
src/reference_counting.rs:5
Functionhurray_for_sets
()
src/sets.rs:4
Functionhurray_for_vecs
()
src/vectors.rs:4
Functioninput_plus_one
`number` is user input, so initially is given as a string.
src/let_bindings.rs:7
Functionkeys
Now we should be equiped to understand the difference between `String` and `&str`: - `String` *owns* string data. It's a data type that can be moved o
src/strings_revisited.rs:7
Functionlets_borrow
()
src/borrowing.rs:3
Functionlongest
This one poses an issue: From which argument do we borrow?
src/lifetime_elision.rs:11
Functionmain
()
src/main.rs:133
Functionmildly_optimistic_for_callbacks
()
src/callbacks.rs:10
Functionmy_function
(a: i64)
src/if_else.rs:6
Functionmy_function
()
src/mutable_methods.rs:12
Methodnew
"Constructor" (static method):
src/struct_impl.rs:7
Methodnew
()
src/lifetimes.rs:13
Functionone_plus_one
One entirely over-engineered function.
src/move_semantics.rs:4
Functionone_plus_one
Another entirely over-engineered function.
src/mutability.rs:4
Functionother_function
(input: MyEnum)
src/if_else.rs:17
Functionother_function
(input: MyEnum)
src/match.rs:5
Functionpositive_sum
(x: i32, y: i32)
src/option_and_result_conversions.rs:17
Methodprint_foo
Regular methods take a reference to `self`:
src/struct_impl.rs:12
Functionprint_item
Rather than using generics, we can pass "trait objects" directly with the `dyn` keyword. The difference? With generics we use static dispatch, while `
src/impl_and_dyn.rs:7
Functionprint_parsed_numbers
()
src/split_examples.rs:12
Functionsubstr
This works without specifying lifetimes, but they're still there... it's just that the compiler allows you to *elide them because it can trivially fig
src/lifetime_elision.rs:6
Functiontrouble_in_paradise
Why can we use `a` twice in the above function? But not in this one?
src/move_semantics.rs:12
Functiontuple_function
# Example: Function with multiple return values:
src/functions_and_tuples.rs:9