diff --git a/exercises/pointers/README.md b/exercises/pointers/README.md new file mode 100644 index 00000000..1f3aa56e --- /dev/null +++ b/exercises/pointers/README.md @@ -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) diff --git a/exercises/pointers/pointers1.rs b/exercises/pointers/pointers1.rs new file mode 100644 index 00000000..07690e21 --- /dev/null +++ b/exercises/pointers/pointers1.rs @@ -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); +} \ No newline at end of file diff --git a/info.toml b/info.toml index ee4ab2f6..47e5ab1d 100644 --- a/info.toml +++ b/info.toml @@ -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` +"""