我正在使用webmatrix,razor语法和cshtml文件.
我有一个数据库记录的"编辑"页面,其中包含一个选择框(id ="selStatus").我试图根据正在编辑的当前记录的值动态设置选择框的"选定"值.
我在本地var中有当前值或其索引但我似乎无法将此值赋给select.
if (currentStatus=="Completed"){
selStatus.options[1].selected=true;
}
Run Code Online (Sandbox Code Playgroud)
RES =错误:当前上下文中不存在名称"selStatus".
我错过了一些明显的东西,但似乎无法得到它.任何想法都赞赏.谢谢
在像C#这样的语言中,给出这个代码(我没有await
故意使用关键字):
async Task Foo()
{
var task = LongRunningOperationAsync();
// Some other non-related operation
AnotherOperation();
result = task.Result;
}
Run Code Online (Sandbox Code Playgroud)
在第一行中,long操作在另一个线程中运行,并Task
返回a(即未来).然后,您可以执行另一个与第一个并行运行的操作,最后,您可以等待操作完成.我认为,这也是行为async
/ await
在Python,JavaScript等
另一方面,在Rust中,我在RFC中读到:
Rust的期货与其他语言的期货之间的根本区别在于,除非进行调查,否则Rust的期货不会做任何事情.整个系统是围绕这个建立的:例如,取消正在降低未来正是出于这个原因.相比之下,在其他语言中,调用异步fn会旋转一个立即开始执行的未来.
在这种情况下,是什么目的async
/ await
鲁斯特?看到其他语言,这种表示法是一种运行并行操作的便捷方式,但是如果调用async
函数没有运行任何东西,我无法看到它在Rust中是如何工作的.
我有一个包含单个变量的变量char
.我想将其转换char
为大写.但是,该to_uppercase
函数返回rustc_unicode::char::ToUppercase
结构而不是a char
.
给定一个像这样创建的网站对象
import {Map} from 'immutable'
const website = new Map({name: 'My Website', url: 'http://www.myw.fr'})
Run Code Online (Sandbox Code Playgroud)
我怎么能声明一个websiteType,它将是一个包含完全给定属性的地图.我知道我能做到:
declare type websiteType = Map<string,string>
Run Code Online (Sandbox Code Playgroud)
但我想更具体一点,并声明一个必须包含属性name
和url
类型的地图string
.
它甚至可能吗?
我不完全了解生命,但我认为b
生命将在此之前结束self
.
那么,如何编辑这段代码呢?我在内存中复制一些东西吗?如果我创建一个新实例,这个生命周期必须遵循这种情况.
pub struct Formater {
layout: &'static str,
}
impl Formater {
pub fn new(layout: &'static str) -> Formater {
let regex = Regex::new(r"%\{([a-z]+)(?::(.*?[^\\]))?\}").unwrap();
let b = regex.replace_all(layout, "{}");
return Formater {
layout: &b,
};
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
error: `b` does not live long enough
--> src/format.rs:16:22
|
16 | layout: &b,
| ^ does not live long enough
17 | };
18 | }
| - borrowed value only lives until here
|
= …
Run Code Online (Sandbox Code Playgroud) use std::fs::File;
fn main() {
let tmp = "tmp.zip";
let save_to_tmp_file = || {
let mut tmp_zip = File::create(tmp)?;
Ok(())
};
save_to_tmp_file();
}
Run Code Online (Sandbox Code Playgroud)
(操场)
构建错误:
use std::fs::File;
fn main() {
let tmp = "tmp.zip";
let save_to_tmp_file = || {
let mut tmp_zip = File::create(tmp)?;
Ok(())
};
save_to_tmp_file();
}
Run Code Online (Sandbox Code Playgroud) 如何在 Rust 中定义一个公共结构,其中所有字段都是公共的,而不必pub
在每个字段前重复修饰符?
一个pub_struct
宏将是理想的:
pub_struct! Foo {
a: i32,
b: f64,
// ...
}
Run Code Online (Sandbox Code Playgroud)
这相当于:
pub struct Foo {
pub a: i32,
pub b: f64,
//...
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序宏,它工作正常,但我无法以符合人体工程学的方式报告错误.使用panic!
"工作"但不优雅,并且不会很好地向用户显示错误消息.
我知道我可以在解析a时报告错误TokenStream
,但是在解析之后我需要在遍历AST时产生错误.
宏调用如下所示:
attr_test! {
#[bool]
FOO
}
Run Code Online (Sandbox Code Playgroud)
并应输出:
const FOO: bool = false;
Run Code Online (Sandbox Code Playgroud)
这是宏代码:
extern crate proc_macro;
use quote::quote;
use syn::parse::{Parse, ParseStream, Result};
use syn::{Attribute, parse_macro_input, Ident, Meta};
struct AttrTest {
attributes: Vec<Attribute>,
name: Ident,
}
impl Parse for AttrTest {
fn parse(input: ParseStream) -> Result<Self> {
Ok(AttrTest {
attributes: input.call(Attribute::parse_outer)?,
name: input.parse()?,
})
}
}
#[proc_macro]
pub fn attr_test(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
let test: AttrTest = parse_macro_input!(tokens);
let name = test.name;
let first_att …
Run Code Online (Sandbox Code Playgroud) 我有一个返回的函数,impl Trait
所以我无权访问具体的返回类型。我需要使用该函数的返回值作为特征中的关联类型。我怎么做?
这是一个简化的示例:
fn f() -> impl Iterator<Item = u8> {
std::iter::empty()
}
struct S;
impl IntoIterator for S {
type Item = u8;
type IntoIter = (); // What can I write here ?
fn into_iter(self) -> Self::IntoIter {
f()
}
}
Run Code Online (Sandbox Code Playgroud)
甚至有可能做到(无需装箱迭代器)?
我是 Rust 的新手,正在尝试传播要在调用函数中处理的错误。从官方 Rust 书中,我读到 Result 'Box< dyn Error>' 用于表示捕获任何类型的错误,但我读得还不够深入,无法理解它的实际工作原理。
我有一个函数叫:
fn foo() -> Result<String, Box<dyn Error>> {
Command::new("an_executable")
.args(&["-p", path])
.output()?;
if condition {
return Err("Error...");
}
// Do stuff, return String
}
Run Code Online (Sandbox Code Playgroud)
因此,有人可以解释如果该返回类型满足条件,我应该如何返回错误。我是否必须更改返回类型或只返回不同的内容。在这种情况下,RUST 标准是什么?
当前编译错误是 Err("Error...") 与返回类型不匹配
rust ×8
rust-macros ×2
async-await ×1
asynchronous ×1
borrowing ×1
closures ×1
flowtype ×1
future ×1
generics ×1
immutable.js ×1
javascript ×1
lifetime ×1
public ×1
razor ×1
struct ×1
syntax ×1
traits ×1
webmatrix ×1