代码很幼稚:
use std::time;
fn main() {
const NUM_LOOP: u64 = std::u64::MAX;
let mut sum = 0u64;
let now = time::Instant::now();
for i in 0..NUM_LOOP {
sum += i;
}
let d = now.elapsed();
println!("{}", sum);
println!("loop: {}.{:09}s", d.as_secs(), d.subsec_nanos());
}
Run Code Online (Sandbox Code Playgroud)
输出是:
$ ./test.rs.out
9223372036854775809
loop: 0.000000060s
$ ./test.rs.out
9223372036854775809
loop: 0.000000052s
$ ./test.rs.out
9223372036854775809
loop: 0.000000045s
$ ./test.rs.out
9223372036854775809
loop: 0.000000041s
$ ./test.rs.out
9223372036854775809
loop: 0.000000046s
$ ./test.rs.out
9223372036854775809
loop: 0.000000047s
$ ./test.rs.out
9223372036854775809
loop: 0.000000045s
Run Code Online (Sandbox Code Playgroud)
该计划几乎立即结束.我还在C中使用for循环编写了一个等效代码,但它运行了很长时间.我想知道是什么让Rust代码如此之快.
C代码:
#include <stdint.h> …Run Code Online (Sandbox Code Playgroud) 我这里有一个很小的操场例子
fn main() {
let l = Some(3);
match &l {
None => {}
Some(_x) => {} // x is of type &i32
}
}
Run Code Online (Sandbox Code Playgroud)
我正在进行模式匹配&Option,如果Some(x)用作分支,为什么是xtype &i32?
我是Rust的新手。我需要在for循环之前创建一个向量。运行循环。在for循环内更改向量。然后在for循环之后更改向量。
我尝试了以下代码,并尝试使用不可变借位,但两者均无效。
fn main() {
let mut vec1 = vec![4, 5];
vec1.push(6);
for i in vec1 {
if i % 2 == 0 {
vec1.push(7);
}
}
vec1.push(8);
println!("vec1={:?}", vec1);
}
Run Code Online (Sandbox Code Playgroud)
我希望在for循环内和之后编译和更改向量。但它显示此错误消息:
fn main() {
let mut vec1 = vec![4, 5];
vec1.push(6);
for i in vec1 {
if i % 2 == 0 {
vec1.push(7);
}
}
vec1.push(8);
println!("vec1={:?}", vec1);
}
Run Code Online (Sandbox Code Playgroud)
您能解释为什么会发生移动吗?你可以编译吗?
我是Rust的新手,在引用和所有权的概念上遇到了麻烦。我只想重新分配一个数组,但是遇到了错误。我尝试了以下方法:
fn change(a: &mut [i64; 3]) {
a = [5, 4, 1];
}
Run Code Online (Sandbox Code Playgroud)
但出现以下错误:
--> main.rs:6:7
|
6 | a = [5, 4, 1];
| ^^^^^^^^^
| |
| expected mutable reference, found array of 3 elements
| help: consider mutably borrowing here: `&mut [5, 4, 1]`
|
= note: expected type `&mut [i64; 3]`
Run Code Online (Sandbox Code Playgroud)
我尝试将添加&mut到数组中,但是出现了一个全新的错误。有人可以指出我正确的方向吗?
对于以下 Rust 程序:
fn main() {
let foo = "test".to_string();
if false {
let _bar = foo; // value moved to _bar
}
println!("{}", foo);
}
Run Code Online (Sandbox Code Playgroud)
运行时出现这个错误:
error[E0382]: borrow of moved value: `foo`
--> src\main.rs:6:20
|
2 | let foo = "test".to_string();
| --- move occurs because `foo` has type `std::string::String`, which does not implement the `Copy` trait
3 | if false {
4 | let _bar = foo; // value moved to _bar
| --- value moved here
5 …Run Code Online (Sandbox Code Playgroud) 我需要从某个网页下载 HTML 代码。完成这项任务的最佳方法是什么?据我所知,现在很少有适用于 Rust 的 Web 框架,而 hyper 是大多数人使用的框架吗?但是在搜索它的文档后,我找不到方法。我得到的最接近的是这个
extern crate hyper;
use hyper::Client;
fn main() {
let client = Client::new();
let res = client.get("http://www.bloomberg.com/")
.send()
.unwrap();
println!("{:?}", res);
}
Run Code Online (Sandbox Code Playgroud)
但它返回Response,它似乎不包含来自 HTML 正文的任何代码。
我无法在筛网实施中发现错误.我的测试显示以下错误.
// Expected
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
// Mine
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49]
Run Code Online (Sandbox Code Playgroud)
有人可以指出我做错了吗?我意识到这与最终条件有关,但我无法弄明白
/// Find all prime numbers less than `n`.
/// For example, `sieve(7)` should return `[2, 3, 5]`
pub fn sieve(n: u32) -> Vec<u32> {
// Check assert and then populate vector
assert!(n > 1, "Error n is less than 1");
let mut is_prime: …Run Code Online (Sandbox Code Playgroud) 鉴于此代码段:
pub fn verse(start_bottles: i32) -> String {
let mut song_template: String = "%1 of beer on the wall, %2 of beer.\n%3, %4 of beer on the wall.\n".to_string();
match start_bottles {
0 => lyrics_no_bottles(&mut song_template),
1 => lyrics_one_bottle(&mut song_template),
2 => lyrics_two_bottles(&mut song_template),
_ => lyrics_more_bottles(&mut song_template, start_bottles)
}
song_template
}
pub fn sing(first: i32, last: i32) -> String {
let mut song: String = "".to_string();
for num in (8..6).rev() {
song = verse(1);
}
song
}
Run Code Online (Sandbox Code Playgroud)
当我输出verse(1)它工作正常 …
我正在尝试一些特定的构建器模式.请不要介意这里的设计 - 这是一个试用版.
我有一个Renderer产生方法的类型set_shape.给调用的参数类型应该实现空特征IsRenderable.然后,方法的登录应该在结构类型之间有所不同,Rectangle并且Circle两者都实现了IsRenderable特征.不要为返回类型而烦恼.
#[derive(Clone, Copy)]
pub struct Rectangle {
pub origin: Point,
pub height: usize,
pub width: usize,
}
trait IsRenderable {}
impl IsRenderBuilder for Rectangle {}
impl<'a> Renderer<'a> {
// logic that needs lifetime 'a
pub fn set_shape<T: IsRenderable>(shape: T) -> Box<IsRenderBuilder> {
match shape {
_ => panic!("WTF!"),
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情. PSEUDO:
match shape {
Rectangle => return RectangleShapeBuilder,
Circle => return CircleShapeBuilder, …Run Code Online (Sandbox Code Playgroud) macro_rules! call_on_self {
($F:ident) => {
self.$F()
}
}
struct F;
impl F {
fn dummy(&self) {}
fn test(&self) {
call_on_self!(dummy);
}
}
Run Code Online (Sandbox Code Playgroud)
以上不起作用:
error[E0424]: expected value, found module `self`
--> src/lib.rs:3:9
|
3 | self.$F()
| ^^^^ `self` value is a keyword only available in methods with `self` parameter
...
11 | call_on_self!(dummy);
| --------------------- in this macro invocation
Run Code Online (Sandbox Code Playgroud)
我希望生成宏
macro_rules! call_on_self {
($F:ident) => {
self.$F()
}
}
struct F;
impl F {
fn dummy(&self) {}
fn test(&self) …Run Code Online (Sandbox Code Playgroud)