1
0
Fork 0
mirror of https://github.com/rust-lang/rustlings.git synced 2024-05-11 00:46:09 +02:00

Compare commits

...

3 Commits

Author SHA1 Message Date
exdx a2857bf997
Merge 5bd2852ee5 into 71053101c3 2024-04-25 19:19:39 +09:00
mo8it 71053101c3 Add --locked 2024-04-24 13:28:25 +02:00
denton 5bd2852ee5
feat: add refcell1 exercise
Signed-off-by: denton <denton24646@gmail.com>
2023-12-02 14:28:55 -05:00
5 changed files with 70 additions and 4 deletions

View File

@ -74,13 +74,13 @@ If you get a permission denied message, you might have to exclude the directory
## Manually
Basically: Clone the repository at the latest tag, run `cargo install --path .`.
Basically: Clone the repository at the latest tag, run `cargo install --locked --path .`.
```bash
# find out the latest version at https://github.com/rust-lang/rustlings/releases/latest (on edit 5.6.1)
git clone -b 5.6.1 --depth 1 https://github.com/rust-lang/rustlings
cd rustlings
cargo install --force --path .
cargo install --locked --force --path .
```
If there are installation errors, ensure that your toolchain is up to date. For the latest, run:

View File

@ -0,0 +1,52 @@
// refcell1.rs
//
// Interior mutability is a design pattern in Rust that allows you to mutate
// data even when there are immutable references to that data;
// normally, this action is disallowed by the borrowing rules.
// The RefCell<T> type represents single ownership over the data it holds.
// Recall the borrowing rules in Rust:
// 1. At any given time, you can have either (but not both) one mutable
// reference or any number of immutable references.
// 2. References must always be valid.
// With references and Box<T>, the borrowing rules’ invariants are enforced at
// compile time. With RefCell<T>, these invariants are enforced at runtime.
// With references, if you break these rules, you’ll get a compiler error.
// With RefCell<T>, if you break these rules, your program will panic and exit.
// The RefCell<T> type is useful when you’re sure your code follows the
// borrowing rules but the compiler is unable to understand and guarantee that.
// I AM NOT DONE
use std::cell::RefCell;
#[derive(Debug)]
#[allow(dead_code)]
struct User {
name: RefCell<String>,
}
#[allow(dead_code)]
impl User {
fn name(&self) -> String {
self.name.borrow().to_string()
}
// Note: do not use &mut self!
fn set_name(&self, name: String) {
todo!()
}
}
fn main() {
let u = User {
name: RefCell::new("Alice".to_string()),
};
println!("My name is {}!", *u.name.borrow());
let new_name = "Bob".to_string();
u.set_name(new_name.clone());
println!("My name is {}!", *u.name.borrow());
}

View File

@ -1103,6 +1103,20 @@ Check out https://doc.rust-lang.org/std/borrow/enum.Cow.html for documentation
on the `Cow` type.
"""
[[exercises]]
name = "refcell1"
path = "exercises/19_smart_pointers/refcell1.rs"
mode = "compile"
hint = """
Remember that RefCell<T> allows for an immutable object to be modified.
Use the .borrow_mut() method on the RefCell to get a mutable reference to
the underlying data.
See https://doc.rust-lang.org/book/ch15-05-interior-mutability.html for more
information on RefCell.
"""
# THREADS
[[exercises]]

View File

@ -78,7 +78,7 @@ Set-Location $path
git checkout -q tags/$version
Write-Host "Installing the 'rustlings' executable..."
cargo install --force --path .
cargo install --locked --force --path .
if (!(Get-Command rustlings -ErrorAction SilentlyContinue)) {
Write-Host "WARNING: Please check that you have '~/.cargo/bin' in your PATH environment variable!"
}

View File

@ -165,7 +165,7 @@ echo "Checking out version $Version..."
git checkout -q ${Version}
echo "Installing the 'rustlings' executable..."
cargo install --force --path .
cargo install --locked --force --path .
if ! [ -x "$(command -v rustlings)" ]
then