-
-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot move out of borrowed content in for loop #107
Comments
This is a fine place to post this! So Askama's |
Thanks for your answer, Rust community is not a lie :) I tried the match &self.certificate {
Some(cert) => {
for (_loop_index, entry) in (&cert.subject_name().entries()).into_iter().enumerate() {
writer.write_str("\n\n ")?;
}
}
None => {
writer.write_str("\n ")?;
}
} Removing the leading |
You can try doing it like this:
But I'm not sure it'll work... In general, you cannot remove the |
Tried that as well, same error. |
I think that's probably the best workaround for now. 👍 |
I am also running into this issue. My code is trying to create an iterator in the template:
|
Just wanted to follow-up on this issue with a similar problem. Given the following structs: pub struct Coursework {
pub instructor: String,
pub name: String,
pub semester: String,
pub short: String,
}
#[derive(Debug, Template)]
#[template(path = "markdown/courses.md", print = "code")]
pub struct MarkdownCoursesTemplate {
pub coursework: Vec<Coursework>,
} and the following template ## Coursework
<table class="table table-hover">
{% for course in coursework %}
<tr>
<td class='col-md-1'>{{ course.semester }}</td>
<td><strong>{{ course.name }}</strong> ({{ course.short }}), {{ course.instructor }}</td>
</tr>
{% endfor %}
</table> The generated code is impl ::askama::Template for MarkdownCoursesTemplate {
fn render_into(&self, writer: &mut ::std::fmt::Write) -> ::askama::Result<()> {
writer.write_str("## Coursework\n\n<table class=\"table table-hover\">")?;
writer.write_str("\n")?;
for (_loop_index, course) in (&self.coursework).into_iter().enumerate() {
writer.write_str("\n")?;
writer.write_str("<tr>\n <td class=\'col-md-1\'>")?;
write!(writer, "{}", &{ course.semester })?;
writer.write_str("</td>\n <td><strong>")?;
write!(writer, "{}", &{ course.name })?;
writer.write_str("</strong> (")?;
write!(writer, "{}", &{ course.short })?;
writer.write_str("),")?;
writer.write_str(" ")?;
write!(writer, "{}", &{ course.instructor })?;
writer.write_str("</td>\n</tr>")?;
writer.write_str("\n")?;
}
writer.write_str("\n")?;
writer.write_str("</table>")?;
Ok(())
}
fn extension(&self) -> Option<&str> {
Some("md")
}
} This fails compilation with
because of Had the generated code been impl ::askama::Template for MarkdownCVTemplate {
fn render_into(&self, writer: &mut ::std::fmt::Write) -> ::askama::Result<()> {
writer.write_str("## Coursework\n\n<table class=\"table table-hover\">")?;
writer.write_str("\n")?;
for (_loop_index, course) in (&self.coursework).into_iter().enumerate() {
writer.write_str("\n")?;
writer.write_str("<tr>\n <td class=\'col-md-1\'>")?;
write!(writer, "{}", course.semester)?; // <--
writer.write_str("</td>\n <td><strong>")?;
write!(writer, "{}", course.name)?; // <--
writer.write_str("</strong> (")?;
write!(writer, "{}", course.short)?; // <--
writer.write_str("),")?;
writer.write_str(" ")?;
write!(writer, "{}", course.instructor)?; // <--
writer.write_str("</td>\n</tr>")?;
writer.write_str("\n")?;
}
writer.write_str("\n")?;
writer.write_str("</table>")?;
Ok(())
}
fn extension(&self) -> Option<&str> {
Some("md")
}
} it would have compiled and I believe would have worked as I expected. Coming from using Jinja in Python, I suspect that there is way to structure my code such that this works. Is there a recommended workaround for this type of scenario? Must these variables be copyable? |
@lukehsiao I think that is essentially the problem from #132, can you comment there and try the suggested fix? |
@djc: what's the purpose of I know very little about askama internals, so there must be a good reason not to translate loops to just loops, what is it? |
They are translated to just loops. The reason for this issue (IIRC) is that |
Actually, this is not quite the case. If the purpose of At the moment, the example above fails because How about the simpler translation where you don't prefix all variables with In my example above, I would write |
Because that adds a bunch of boiler plate for the common case. I don't have the exact issue here paged in at the moment and it sounds like you do, so maybe you can explain in more detail why your |
This is because, AFAICT, in the current implementation However,
I'm not sure about that, since it just adds a Also, doing less translation would give you the extra benefit of generating Rust code with the correct source positions, resulting in nicer error messages. |
You can try to add Sorry, I'm not going to change the syntax to always require |
Another example use case which breaks: Types with multiple iterators yielding different types of iterators have to be disambiguated by type annotation which is not possible afaik struct Z{
children: Vec<Vec<u8>>,
}
impl Z {
pub fn iter_vec() -> { unimplemented!()}
pub fn iter_items() -> { unimplemented!()}
}
|
I think this is related to #221 |
I think we could fix this by using autoref-based specialization to dispatch to proper handling in Askama. If someone wants to take a whack at implementing this, I'd be happy to provide further guidance and review! |
Hi there,
I am probably not posting this in the right place but I seek help using this library.
I am trying to iterate over a list of x509Name's using the following code in my template :
certificate
being an instance of X509. I have the following error :cannot move out of borrowed content
. I am pretty new to rust, this must be something easy but I have not yet full comprehension of the concept of ownership..The text was updated successfully, but these errors were encountered: