我刚开始学习 Rust,我经常有一些我找不到任何答案的问题。我真的不知道如何以及在哪里发布我的问题,所以我会尝试。
我开始阅读网站上的 Rust 文档,并做了猜谜游戏示例。
我意识到match cmp循环内的表达式可以变成一个语句,一切仍然有效。所以我想知道为什么和哪个版本应该是首选?
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1, 101);
loop {
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
} …Run Code Online (Sandbox Code Playgroud) 我有一个组件如下,我试图动态提供我的 svg viewBox 维度,从我的 bootstrap 注入到 main.ts。
import {Component} from 'angular2/core';
import {CircleComponent} from './circle.component';
import {Circles} from './circles.service';
@Component({
selector: 'my-app',
styleUrls: ['./app/app.component.css'],
directives: [CircleComponent],
providers: [Circles],
template: `
<svg [attr.viewBox]="getViewBox()" preserveAspectRatio="xMidYMid meet">
<svg:g my-circle *ngFor="#circle of circles.circles" [circle]="circle" />
</svg>
`
})
export class AppComponent{
static parameters = [Circles, 'canvasWidth', 'canvasHeight'];
constructor(circles: Circles, canvasWidth: Number, canvasHeight: Number) {
this.circles = circles;
this.width = canvasWidth;
this.height = canvasHeight;
}
function getViewBox(): String {
return `0 0 ${this.width} ${this.height}`;
} …Run Code Online (Sandbox Code Playgroud)