Code
Hub
Workspaces
Following
Trending
Connect
MCP
copy
Create free account
hub
/
github.com/arendjr/rust-for-ts-devs
/ functions
Functions
59 in github.com/arendjr/rust-for-ts-devs
⨍
Functions
59
◇
Types & classes
11
↓ 2 callers
Function
create_struct_with_foo
# Example
src/structs.rs:12
↓ 2 callers
Function
divide
`Result<T, E>` is mainly used to replace exceptions.
src/option_and_result.rs:14
↓ 2 callers
Function
modify
In Rust terminology, `modify()` *borrows* a mutable reference.
src/borrowing.rs:18
↓ 2 callers
Function
positive_n_result
(n: i32)
src/option_and_result_conversions.rs:5
↓ 2 callers
Function
print
(b: &MyCopyableStruct)
src/borrowing.rs:22
↓ 2 callers
Function
process_and_print
Instead, we commonly use trait bounds:
src/function_traits.rs:2
↓ 2 callers
Function
process_and_print
(string: &str, processor: ProcessStr)
src/callbacks.rs:5
↓ 1 callers
Function
modify
In Rust terminology, `modify()` *borrows* a mutable reference.
src/references.rs:13
↓ 1 callers
Function
modify
(mut a: MyCopyableStruct)
src/mutability.rs:18
↓ 1 callers
Function
modify_string
(string: Rc<Cell<String>>)
src/cells.rs:13
↓ 1 callers
Function
positive_n
`Option<T>` is used in place of `T | null` or `T | undefined`.
src/option_and_result.rs:5
↓ 1 callers
Function
print_string
(string: Rc<String>)
src/reference_counting.rs:18
↓ 1 callers
Function
quiz
Quiz time! (Use `cargo run` to see the answer)
src/mutability.rs:12
↓ 1 callers
Function
second_quiz
Let's do another quiz! Same as the first, but now we pass by reference:
src/references.rs:5
↓ 1 callers
Method
set_foo
(&mut self, string: &str)
src/mutable_methods.rs:7
↓ 1 callers
Function
split
This one shows almost everything we learned today in one signature:
src/split_examples.rs:2
↓ 1 callers
Function
split_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 callers
Function
unit_function
# Example: Function without return value:
src/functions_and_tuples.rs:4
Function
add
# Example
src/numbers.rs:16
Function
add
(a: T, b: T)
src/where_clauses.rs:14
Method
add
(self, rhs: Self)
src/traits.rs:18
Function
calling_clone_solves_our_woes
()
src/clone_trait.rs:9
Function
clone
(item: &T)
src/generic_functions.rs:5
Function
clone_and_print
(item: &T)
src/where_clauses.rs:5
Function
clone_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
Function
concatenate
# Example 1
src/strings.rs:10
Function
divide_option
(x: i32, y: i32)
src/option_and_result_conversions.rs:9
Function
divide_or_zero
(x: i32, y: i32)
src/option_and_result_conversions.rs:13
Method
from_points
(points: &'a [Point<f64>])
src/lifetimes.rs:17
Function
get_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
Function
greeting
# Example 2
src/strings.rs:16
Function
hurray
()
src/copy_trait.rs:11
Function
hurray_for_closures
()
src/function_traits.rs:12
Function
hurray_for_maps
()
src/maps.rs:3
Function
hurray_for_rc
()
src/cells.rs:5
Function
hurray_for_rc
()
src/reference_counting.rs:5
Function
hurray_for_sets
()
src/sets.rs:4
Function
hurray_for_vecs
()
src/vectors.rs:4
Function
input_plus_one
`number` is user input, so initially is given as a string.
src/let_bindings.rs:7
Function
keys
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
Function
lets_borrow
()
src/borrowing.rs:3
Function
longest
This one poses an issue: From which argument do we borrow?
src/lifetime_elision.rs:11
Function
main
()
src/main.rs:133
Function
mildly_optimistic_for_callbacks
()
src/callbacks.rs:10
Function
my_function
(a: i64)
src/if_else.rs:6
Function
my_function
()
src/mutable_methods.rs:12
Method
new
"Constructor" (static method):
src/struct_impl.rs:7
Method
new
()
src/lifetimes.rs:13
Function
one_plus_one
One entirely over-engineered function.
src/move_semantics.rs:4
Function
one_plus_one
Another entirely over-engineered function.
src/mutability.rs:4
Function
other_function
(input: MyEnum)
src/if_else.rs:17
Function
other_function
(input: MyEnum)
src/match.rs:5
Function
positive_sum
(x: i32, y: i32)
src/option_and_result_conversions.rs:17
Method
print_foo
Regular methods take a reference to `self`:
src/struct_impl.rs:12
Function
print_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
Function
print_parsed_numbers
()
src/split_examples.rs:12
Function
substr
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
Function
trouble_in_paradise
Why can we use `a` twice in the above function? But not in this one?
src/move_semantics.rs:12
Function
tuple_function
# Example: Function with multiple return values:
src/functions_and_tuples.rs:9