MCPcopy Create free account

hub / github.com/RalfJung/rust-101 / functions

Functions176 in github.com/RalfJung/rust-101

↓ 25 callersMethoditer
@ Notice that when we write the type of `iter`, we don't actually have to give the lifetime @ parameter of `Iter`. Just as it is the case with functio
src/part09.rs:59
↓ 10 callersMethodclone
(&self)
src/part05.rs:85
↓ 9 callersMethodclone
(&self)
solutions/src/bigint.rs:135
↓ 7 callersMethodcall
(&self, val: i32)
src/part12.rs:41
↓ 4 callersMethodinc1
Increments the number by 1.
solutions/src/bigint.rs:82
↓ 4 callersMethodpush_back
(&mut self, t: T)
solutions/src/list.rs:30
↓ 4 callersMethodregister
Registration works just like last time, except that we are creating an `Rc` now.
src/part12.rs:37
↓ 3 callersMethodcall
And here we call all the stored callbacks.
src/part11.rs:75
↓ 3 callersMethodpush_front
(&mut self, t: T)
solutions/src/list.rs:64
↓ 3 callersFunctionraw_into_box
(r: *mut T)
solutions/src/list.rs:8
↓ 3 callersFunctionvec_min
Compute the minimum element of the vector
solutions/src/vec.rs:12
↓ 2 callersMethodact
(&self, mut a: A)
src/part10.rs:79
↓ 2 callersFunctionbox_into_raw
(b: Box<T>)
solutions/src/list.rs:5
↓ 2 callersMethodcall
(&mut self, val: i32)
solutions/src/callbacks.rs:19
↓ 2 callersMethodincrement
(&self, by: usize)
solutions/src/counter.rs:14
↓ 2 callersMethodincrement
The core operation is, of course, `increment`.
src/part15.rs:47
↓ 2 callersMethoditer_mut
Next, we are going to provide an iterator. @ This function just creates an instance of `IterMut`, the iterator type which does the actual @ work.
src/part16.rs:132
↓ 2 callersMethodnext
(&mut self)
src/part09.rs:40
↓ 2 callersMethodprint
(self)
solutions/src/vec.rs:75
↓ 2 callersMethodprint
(self)
src/part02.rs:128
↓ 2 callersFunctionread_vec
()
solutions/src/vec.rs:82
↓ 2 callersMethodregister
(&mut self, callback: F)
solutions/src/callbacks.rs:14
↓ 2 callersFunctionrun
With the operations of the three threads defined, we can now implement a function that performs grepping according to some given options.
src/part13.rs:109
↓ 2 callersFunctionsort
@ `[T]` is the type of an (unsized) *array*, with elements of type `T`. All this means is that @ there's a contiguous region of memory, where a bunch
src/part14.rs:23
↓ 2 callersFunctionvec_inc
@ As an example, consider a function which increments every element of a vector by 1. @ The type `&mut Vec<i32>` is the type of mutable references to
src/part04.rs:111
↓ 2 callersFunctionvec_min
@ So, let's re-write `vec_min` to work on a shared reference to a vector, written `&Vec<i32>`. @ I also took the liberty to convert the function from
src/part04.rs:67
↓ 2 callersFunctionvec_min
@ Next, we write `vec_min` as a generic function over a type `T` that we demand to satisfy the `Minimum` trait. @ This requirement is called a *trait
src/part02.rs:88
↓ 1 callersMethodact_v1
(&self, mut a: A)
src/part10.rs:24
↓ 1 callersFunctionbox_into_raw
@ The case is slightly different for `box_into_raw`: Converting a `Box` to a raw pointer is @ always safe. It just drops some information. Hence we ke
src/part16.rs:90
↓ 1 callersFunctiondemo
Time for a demo!
src/part12.rs:50
↓ 1 callersMethoddo_action
Here we perform the actual printing of the prefix and the digit. We're not making use of our ability to change `self` here, but we could replace the p
src/part10.rs:45
↓ 1 callersFunctionfilter_lines
(options: Arc<Options>, in_channel: Receiver<Line>, out_channel: SyncSender<Line>)
solutions/src/rgrep.rs:48
↓ 1 callersFunctionfilter_lines
The second function filters the lines it receives through `in_channel` with the pattern, and sends matches via `out_channel`.
src/part13.rs:67
↓ 1 callersMethodget
(&self)
solutions/src/counter.rs:26
↓ 1 callersMethodget
The function `get` returns the current value of the counter.
src/part15.rs:70
↓ 1 callersFunctionget_options
()
solutions/src/rgrep.rs:112
↓ 1 callersFunctionget_options
This function extracts the rgrep options from the command-line arguments.
src/part14.rs:130
↓ 1 callersFunctionhead
@ The function `head` demonstrates how that could work: It returns a reference to the first @ element of a vector if it is non-empty. The type of the
src/part06.rs:108
↓ 1 callersMethoditer_mut
(&mut self)
solutions/src/list.rs:108
↓ 1 callersMethodmin
(self, b: Self)
solutions/src/vec.rs:68
↓ 1 callersMethodmin
This is essentially the solution to 06.1.
solutions/src/bigint.rs:149
↓ 1 callersMethodmin
(self, b: Self)
src/part02.rs:118
↓ 1 callersMethodmin
(&'a self, other: &'a Self)
src/part07.rs:41
↓ 1 callersFunctionmin_i32
Now that we reduced the problem to computing the minimum of two integers, let's do that.
src/part00.rs:64
↓ 1 callersFunctionmin_i32
@ Remember that helper function `min_i32`? Rust allows us to define such helper functions @ *inside* other functions. This is just a matter of namespa
src/part01.rs:50
↓ 1 callersMethodmin_try1
(self, other: Self)
src/part06.rs:10
↓ 1 callersFunctionoutput_lines
(options: Arc<Options>, in_channel: Receiver<Line>)
solutions/src/rgrep.rs:83
↓ 1 callersFunctionoutput_lines
The third function performs the output operations, receiving the relevant lines on its `in_channel`.
src/part13.rs:83
↓ 1 callersFunctionoverflowing_add
Add with carry, returning the sum and the carry
solutions/src/bigint.rs:27
↓ 1 callersFunctionoverflowing_add
So, let us write a function to "add with carry", and give it the appropriate type. Notice Rust's native support for pairs.
src/part08.rs:15
↓ 1 callersFunctionoverflowing_sub
Subtract with carry, returning the difference and the carry
solutions/src/bigint.rs:41
↓ 1 callersMethodprint
(self)
src/part01.rs:79
↓ 1 callersMethodprint2
(self)
solutions/src/vec.rs:170
↓ 1 callersMethodprint_f32
(self)
solutions/src/vec.rs:99
↓ 1 callersFunctionprint_number_or_nothing
@ `println!` is again a macro, where the first argument is a *format string*. For @ now, you just need to know that `{}` is the placeholder for a valu
src/part00.rs:96
↓ 1 callersFunctionprint_with_prefix_v1
Finally, this function takes a `BigInt` and a prefix, and prints the digits with the given prefix. @ It does so by creating an instance of `PrintWithS
src/part10.rs:53
↓ 1 callersFunctionraw_into_box
@ We declare `raw_into_box` to be an `unsafe` function, telling Rust that calling this function @ is not generally safe. This grants us the unsafe pow
src/part16.rs:82
↓ 1 callersFunctionread_files
(options: Arc<Options>, out_channel: SyncSender<Line>)
solutions/src/rgrep.rs:37
↓ 1 callersFunctionread_files
The first function reads the files, and sends every line over the `out_channel`.
src/part13.rs:50
↓ 1 callersFunctionread_vec
@ `vec!` is a *macro* (as indicated by `!`) that constructs a constant `Vec<_>` with the given @ elements.
src/part00.rs:85
↓ 1 callersFunctionread_vec
Now we are ready to run our new code. Remember to change `main.rs` appropriately. @ Rust figures out automatically that we want the `T` of `vec_min` t
src/part02.rs:139
↓ 1 callersFunctionread_vec
@ Let's now go over this function line-by-line. First, we call the constructor of `Vec` @ to create an empty vector. As mentioned in the previous part
src/part03.rs:23
↓ 1 callersFunctionread_vec
With our refactored functions and methods, `main` now looks as follows:
src/part01.rs:95
↓ 1 callersFunctionread_vec_f32
()
solutions/src/vec.rs:107
↓ 1 callersMethodregister
Registration simply stores the callback.
src/part11.rs:53
↓ 1 callersMethodregister_generic
@ For this to work, we need to demand that the type `F` does not contain any short-lived @ references. After all, we will store it in our list of call
src/part11.rs:70
↓ 1 callersFunctionrun
(options: Options)
solutions/src/rgrep.rs:134
↓ 1 callersFunctionsort
(data: &mut [T])
solutions/src/rgrep.rs:56
↓ 1 callersFunctionvec_min
Observe how in Rust, the return type comes *after* the arguments.
src/part00.rs:28
↓ 1 callersFunctionvec_min
Let us now refactor `vec_min`.
src/part01.rs:45
↓ 1 callersFunctionwork_on_vector
## Ownership @ What does that mean in practice? Consider the following example.
src/part04.rs:26
Functionabs
Conditionals are also just expressions. This is comparable to the ternary `? :` operator from languages like C.
src/part01.rs:21
Methodadd
(self, rhs: &'a BigInt)
solutions/src/bigint.rs:180
Methodadd
Now we can write the actual function performing the addition.
src/part08.rs:83
Functioncall_constructor
@ Observe how `new` does *not* have a `self` parameter. This corresponds to a `static` method @ in Java or C++. In fact, `new` is the Rust convention
src/part02.rs:56
Functionclone_demo
## Cloning @ If you take a close look at the type of `BigInt::from_vec`, you will notice that it consumes @ the vector `v`. The caller hence loses acc
src/part05.rs:72
Methodcompare_and_inc
(&self, test: usize, by: usize)
solutions/src/counter.rs:19
Functioncompare_big_ints
Now we can compare `BigInt`s. Rust treats `PartialEq` special in that it is wired to the operator `==`: @ That operator can now be used on our numbers
src/part07.rs:96
Functioncompute_stuff
It is even the case that blocks are expressions, evaluating to the last expression they contain.
src/part01.rs:39
Functiondemo_cell
So, let us put our counter in a `Cell`, and replicate the example from the previous part.
src/part12.rs:79
Functiondemo_mut
Now we can repeat the demo from the previous part - but this time, our `CallbacksMut` type can be cloned.
src/part12.rs:175
Methoddrop
(&mut self)
solutions/src/list.rs:133
Methoddrop
The destructor itself is a method which takes `self` in mutably borrowed form. It cannot own `self`, because then the destructor of `self` would be ca
src/part16.rs:205
Methodeq
(&self, other: &BigInt)
solutions/src/bigint.rs:141
Methodeq
(&self, other: &Line)
solutions/src/rgrep.rs:27
Methodeq
(&self, other: &BigInt)
src/part07.rs:60
Functionfilter_vec_by_divisor
And as a final example, one can also collect all elements of an iterator, and put them, e.g., in a vector.
src/part10.rs:151
Methodfmt
(&self, f: &mut fmt::Formatter)
solutions/src/vec.rs:119
Methodfmt
(&self, f: &mut fmt::Formatter)
solutions/src/bigint.rs:173
Methodfmt
(&self, f: &mut fmt::Formatter)
src/part07.rs:134
Methodfor_each
(&mut self, mut f: F)
solutions/src/list.rs:99
Methodfrom_vec
Construct a BigInt from a vector of 64-bit "digits", with the last significant digit being first. Solution to 05.1.
solutions/src/bigint.rs:73
Methodfrom_vec
We can convert any little-endian vector of digits (i.e., least-significant digit first) into a number, by removing trailing zeros. The `mut` declarati
src/part05.rs:61
Methodinc
Increments the number by "by".
solutions/src/bigint.rs:102
Functioninc_print_threshold
Let's say we want to write a function that increments every entry of a `Vec` by some number, then looks for numbers larger than some threshold, and pr
src/part10.rs:126
Methodinto_iter
(self)
src/part09.rs:155
Functioniter_invalidation_demo
@ It turns out that the answer to this question is yes! This particular aspect of the concept of @ lifetimes helps Rust to eliminate the issue of *ite
src/part09.rs:114
Functionmain
()
solutions/src/vec.rs:154
Functionmain
()
solutions/src/main.rs:15
Functionmain
()
solutions/src/rgrep.rs:152
next →1–100 of 176, ranked by callers