Skip to content

Commit

Permalink
adding immutable example
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgift committed Sep 7, 2023
1 parent 1de1f27 commit 7570470
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions immutable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "immutable"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
13 changes: 13 additions & 0 deletions immutable/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
format:
cargo fmt --quiet

lint:
cargo clippy --quiet

test:
cargo test --quiet

run:
cargo run

all: format lint test run
11 changes: 11 additions & 0 deletions immutable/immutable-py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def add_one_to_list(lst):
for i in range(len(lst)):
lst[i] += 1

lst = [1, 2, 3]
add_one_to_list(lst)
print(lst)

# This will modify the list outside of the function
lst[0] += 1
print(lst)
11 changes: 11 additions & 0 deletions immutable/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* An example of an immutable list */

//This will generate a compliation error because mut is not needed
fn main(){
// a vector of immutable integers
let mut v = vec![1, 2, 3];
//print all the elements in the vector
for i in &v {
println!("{}", i);
}
}

0 comments on commit 7570470

Please sign in to comment.