我想创建一个&'static str由重复的字符序列组成的长片,例如abcabcabc...
在Rust中有没有办法通过表达式来实现这一点,例如long_str = 1000 * "abc"在Python中,或者我是否必须在Python中生成它并将其复制/粘贴到Rust代码中?
有人可以解释为什么编译:
fn main() {
let a = vec![1, 2, 3];
println!("{:?}", a[4]);
}
Run Code Online (Sandbox Code Playgroud)
运行时,我得到:
线程''惊慌失措'索引越界:len是3但索引是4',../src/libclections/vec.rs:1132
我有一个非常简单的Rust代码示例,无法编译:
extern crate rustc_serialize;
use rustc_serialize::base64;
fn main() {
let auth = format!("{}:{}", "user", "password");
let auth_b64 = auth.as_bytes().to_base64(base64::MIME);
println!("Authorization string: {}", auth_b64);
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
error[E0599]: no method named `to_base64` found for type `&[u8]` in the current scope
--> src/main.rs:6:36
|
6 | let auth_b64 = auth.as_bytes().to_base64(base64::MIME);
| ^^^^^^^^^
|
= help: items from traits can only be used if the trait is in scope
= note: the following trait is implemented but not in scope, perhaps add a `use` …Run Code Online (Sandbox Code Playgroud) 我的防锈代码在rust-http里面崩溃,我不知道如何调试它.
我运行时遇到此错误:
$ ./target/ecmiwc -i www.google.com -u testuser -v test
task '<main>' failed at 'called `Option::unwrap()` on a `None` value', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libcore/option.rs:262
Run Code Online (Sandbox Code Playgroud)
我想得到一些关于如何找到调用代码的指令Option::unwrap().
如何触发核心文件或是否有其他方法可以获取更多信息?
我的编程经验主要在于动态语言,其中崩溃提供了完整的回溯,并且很容易找到有问题的代码.如何获得与锈相似的信息?
稍后编辑:
根据Steve K的回答,我启用了回溯env但不幸的是,回溯并不是很有用:
$ RUST_BACKTRACE=1 ./target/ecmiwc -i www.google.com -u testuser -v test
task '<main>' failed at 'called `Option::unwrap()` on a `None` value', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libcore/option.rs:262
stack backtrace:
1: 0x10b70c065 - rt::backtrace::imp::write::h471bb232e1b48857jar
2: 0x10b70f1ef - failure::on_fail::h05edea1dadf8fbaaOqr
3: 0x10b715e25 - unwind::begin_unwind_inner::h7c6fecebc6991c8bS5d
4: 0x10b715a5b - unwind::begin_unwind_fmt::h227376fe1e021a36n3d
5: 0x10b7158b2 - rust_begin_unwind
6: 0x10b73769c - failure::begin_unwind::h7d8f396ab219c1bbn5j
7: 0x10b5a2cce - option::Option<T>::unwrap::h6219013626023885255
8: …Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么下面的代码不能并行运行?我想我不明白是怎么thread::scoped工作的..
use std::thread;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::old_io::timer;
fn main() {
let buf = Arc::new(Mutex::new(Vec::<String>::new()));
let res = test(buf);
println!("{:?}", *res.lock().unwrap());
}
fn test(buf: Arc<Mutex<Vec<String>>>) -> Arc<Mutex<Vec<String>>> {
let guards: Vec<_> = (0..3).map( |i| {
let mtx = buf.clone();
thread::scoped(|| {
println!("Thread: {}", i);
let mut res = mtx.lock().unwrap();
timer::sleep(Duration::seconds(5));
res.push(format!("thread {}", i));
});
}).collect();
buf
}
Run Code Online (Sandbox Code Playgroud)
范围函数接受一个参数,一个闭包,由双条||表示.此闭包在由scoped创建的新线程中执行.该方法称为作用域,因为它返回一个'join guard',当它超出范围时将自动加入子线程.因为我们将这些守卫收集到Vec中,并且在我们的程序结束时该向量超出了范围,我们的程序将在完成之前等待每个线程完成.
谢谢
rust ×5