* [rust-dev] Rust Guide 22 and 23
@ 2014-12-05 11:33 Péter Mózes Merl
2014-12-05 13:15 ` Nathan Sizemore
0 siblings, 1 reply; 4+ messages in thread
From: Péter Mózes Merl @ 2014-12-05 11:33 UTC (permalink / raw)
To: rust-dev
[-- Attachment #1: Type: text/plain, Size: 619 bytes --]
Hi Everyone,
after long years of working with scripting languages I have finally
found a strongly-typed language that is worth to learn (for me). Cheers.
The Guide is awesome. There is only one thing I missed and that’s
between chapters 22 and 23, Generics and Traits. The Guide says that we
have to learn about Traits to fix this message:
error: binary operation `==` cannot be applied to type `T`
I think the example how to fix it at the end is missing. I am not sure
whether this is the right place to provide such a feedback, however, I
could not find a link in the Guide.
Thank you.
Péter
[-- Attachment #2: Type: text/html, Size: 945 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [rust-dev] Rust Guide 22 and 23
2014-12-05 11:33 [rust-dev] Rust Guide 22 and 23 Péter Mózes Merl
@ 2014-12-05 13:15 ` Nathan Sizemore
2014-12-05 14:52 ` Péter Mózes Merl
0 siblings, 1 reply; 4+ messages in thread
From: Nathan Sizemore @ 2014-12-05 13:15 UTC (permalink / raw)
To: Péter Mózes Merl; +Cc: rust-dev
[-- Attachment #1: Type: text/plain, Size: 1693 bytes --]
I believe that was just showing an example of the error the compiler will
generate when not using trait constraints. If you read a little further, a
similar error is produced from
fn print_area<T>(shape: T) {
println!("This shape has an area of {}", shape.area());
}
Because T can be any type, we can't be sure that it implements the area method.
But we can add a *trait constraint* to our generic T, ensuring that it does:
fn print_area<T: HasArea>(shape: T) {
println!("This shape has an area of {}", shape.area());
}
If you're wanting to do comparisons, you will probably want to place a
trait constraint on your functions from the following module:
http://doc.rust-lang.org/std/cmp/index.html#traits
Nathan Sizemore
@nathansizemore | 937.823.7229
On Fri, Dec 5, 2014 at 6:33 AM, Péter Mózes Merl <mage@mage.li> wrote:
> Hi Everyone,
>
> after long years of working with scripting languages I have finally found
> a strongly-typed language that is worth to learn (for me). Cheers.
>
> The Guide is awesome. There is only one thing I missed and that’s between
> chapters 22 and 23, Generics and Traits. The Guide says that we have to
> learn about Traits to fix this message:
>
> error: binary operation `==` cannot be applied to type `T`
>
> I think the example how to fix it at the end is missing. I am not sure
> whether this is the right place to provide such a feedback, however, I
> could not find a link in the Guide.
>
> Thank you.
>
> Péter
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
[-- Attachment #2: Type: text/html, Size: 5097 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [rust-dev] Rust Guide 22 and 23
2014-12-05 13:15 ` Nathan Sizemore
@ 2014-12-05 14:52 ` Péter Mózes Merl
2014-12-05 23:59 ` Kai Noda
0 siblings, 1 reply; 4+ messages in thread
From: Péter Mózes Merl @ 2014-12-05 14:52 UTC (permalink / raw)
To: rust-dev
[-- Attachment #1: Type: text/plain, Size: 874 bytes --]
Am 05/12/14 um 14:15 schrieb Nathan Sizemore:
>
> If you're wanting to do comparisons, you will probably want to place a
> trait constraint on your functions from the following
> module: http://doc.rust-lang.org/std/cmp/index.html#traits
>
I tried to write the function (my second Rust code ever). I thought that
returning T makes no sense since the inverse of an integer should be a
float.
Is this the right way?
use std::num;
fn main() {
fn inverse<T: num::NumCast>(x: T) -> Result<f64, String> {
let local: f64 = num::cast(x).unwrap();
if 0f64 == local { return Err("x cannot be zero!".to_string()); }
Ok(1f64 / local)
}
match inverse(5.2f32) {
Ok(n) => println!("{}", n),
Err(s) => println!("{}", s)
}
match inverse(4i) {
Ok(n) => println!("{}", n),
Err(s) => println!("{}", s)
}
}
[-- Attachment #2: Type: text/html, Size: 2608 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [rust-dev] Rust Guide 22 and 23
2014-12-05 14:52 ` Péter Mózes Merl
@ 2014-12-05 23:59 ` Kai Noda
0 siblings, 0 replies; 4+ messages in thread
From: Kai Noda @ 2014-12-05 23:59 UTC (permalink / raw)
To: Péter Mózes Merl; +Cc: rust-dev
[-- Attachment #1: Type: text/plain, Size: 2076 bytes --]
Hi Peter,
I would do in this way:
use std::fmt::Show;
use std::num::{mod, NumCast};
fn inverse<T: NumCast>(x: T) -> Result<f64, String> {
let local = num::cast(x).expect("fail to cast into f64");
if 0. == local { Err("x cannot be zero!".to_string()); }
Ok(1. / local)
}
fn dotest<T: NumCast + Show + Clone> (x: T) {
println!("1/{} => {}", x.clone(), inverse(x));
}
fn main() {
dotest(5.2f32);
dotest(4i32);
dotest(0i);
}
(Disclaimer: it's only three months since I started to learn Rust!)
There is an online compiler, though it's one month old and details of
the language and the stdlib somewhat differ from those of the nightly
version:
http://is.gd/13zBYc (Hit the [evaluate] button)
I think Reddit, StackOverflow and IRC (there's a web interface) are more
active than this list.
Hope this helps.
Kai
野田 開 <nodakai@gmail.com>
2014-12-05 22:52 GMT+08:00 Péter Mózes Merl <mage@mage.li>:
> Am 05/12/14 um 14:15 schrieb Nathan Sizemore:
>
>
> If you're wanting to do comparisons, you will probably want to place a
> trait constraint on your functions from the following module:
> http://doc.rust-lang.org/std/cmp/index.html#traits
>
> I tried to write the function (my second Rust code ever). I thought that
> returning T makes no sense since the inverse of an integer should be a
> float.
>
> Is this the right way?
>
> use std::num;fn main() {
> fn inverse<T: num::NumCast>(x: T) -> Result<f64, String> {
> let local: f64 = num::cast(x).unwrap(); if 0f64 == local { return Err("x cannot be zero!".to_string()); }
> Ok(1f64 / local)
> }
>
> match inverse(5.2f32) {
> Ok(n) => println!("{}", n), Err(s) => println!("{}", s)
> }
>
> match inverse(4i) {
> Ok(n) => println!("{}", n), Err(s) => println!("{}", s)
> }
> }
>
>
>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
[-- Attachment #2: Type: text/html, Size: 4822 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2014-12-05 23:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-05 11:33 [rust-dev] Rust Guide 22 and 23 Péter Mózes Merl
2014-12-05 13:15 ` Nathan Sizemore
2014-12-05 14:52 ` Péter Mózes Merl
2014-12-05 23:59 ` Kai Noda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox