feat: Add pointers1.rs exercise

This commit is contained in:
TheLe0 2020-06-27 12:11:55 -03:00
parent d355927024
commit f929d19516
3 changed files with 50 additions and 0 deletions

View file

@ -0,0 +1,10 @@
### Pointers
Rust is a low level low-level programming language and focus on high level code performance, for this
is necessary a good memory management and this infers on the use of pointers.
Pointers is a primitive type, that allows to access the memory address of a variable, or the value
alocated on an specific address.
#### Book Sections
- [Pointers](https://doc.rust-lang.org/std/primitive.pointer.html)

View file

@ -0,0 +1,30 @@
// pointers1.rs
// Where you pass a variable as parameters, you are not passing that variable
// instance itself, the compiler will make a copy of that variable for the use
// on that scope.
// For manage that you need to work with a pointer variable, because you are
// going to know exactly where the variable was allocated.
// The variables are passing on the parameters as its respective memories
// addresses, not the values itself.
// Make me compile! Execute `rustlings hint pointers1` for hints :)
// I AM NOT DONE
// TODO: Something is wrong on this function body
pub fn change_vals(a: &mut i32, b: &mut i32) {
let c: i32 = *a;
a = *b;
b = c;
}
fn main() {
let mut a: i32 = 5;
let mut b: i32 = 3;
println!("BEFORE {} e {}", a, b);
change_vals(&mut a, &mut b);
println!("AFTER {} e {}", a, b);
}

View file

@ -822,3 +822,13 @@ hint = """
The implementation of FromStr should return an Ok with a Person object,
or an Err with a string if the string is not valid.
This is almost like the `try_from_into` exercise."""
# POINTERS
name = "pointers1"
path = "exercises/pointers/pointers1.rs"
mode = "test"
hint = """
When the function change the variables `a` and `b`,
is trying to change the value, not its address.
Try to reference their addresses `*your_variable`
"""