Skip to content

Commit

Permalink
Add automatic borrowing to let statement
Browse files Browse the repository at this point in the history
Signed-off-by: max <[email protected]>
  • Loading branch information
PizzasBear committed Dec 13, 2023
1 parent 243d4a4 commit a294541
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,14 @@ impl<'a> Generator<'a> {
};

let mut expr_buf = Buffer::new(0);
let borrow_val = !is_copyable(val);
if borrow_val {
expr_buf.write("&(");
}
self.visit_expr(&mut expr_buf, val)?;
if borrow_val {
expr_buf.write(")");
}

let shadowed = self.is_shadowing_variable(&l.var)?;
if shadowed {
Expand Down
14 changes: 14 additions & 0 deletions testing/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,17 @@ fn test_num_literals() {
"[90, -90, 90, 2, 56, 240, 10.5, 10.5, 100000000000, 105000000000]",
);
}

#[derive(askama::Template)]
#[template(source = "{% let word = s %}{{ word }}", ext = "html")]
struct LetBorrow {
s: String,
}

#[test]
fn test_let_borrow() {
let template = LetBorrow {
s: "hello".to_owned(),
};
assert_eq!(template.render().unwrap(), "hello")
}

0 comments on commit a294541

Please sign in to comment.