por valor y por referencia
This commit is contained in:
8
porreferencia/Cargo.toml
Normal file
8
porreferencia/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "porreferencia"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
12
porreferencia/src/main.rs
Normal file
12
porreferencia/src/main.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
fn agregar(a: &i32, b: &i32) -> i32 {
|
||||||
|
//pide que los datos sean referencias
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let v1 = &3; // & referencia
|
||||||
|
let v2 = *v1; // * desreferencia
|
||||||
|
let valor = agregar(v1, &v2);
|
||||||
|
|
||||||
|
println!("{}", valor);
|
||||||
|
}
|
||||||
8
porvalor/Cargo.toml
Normal file
8
porvalor/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "porvalor"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
9
porvalor/src/main.rs
Normal file
9
porvalor/src/main.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
fn agregar(a: i32, b: i32) -> i32 {
|
||||||
|
// como no tiene punto y coma este dato es retornado
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let valor = agregar(3, 5);
|
||||||
|
println!("{}", valor)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user